tycho_execution/encoding/evm/
tycho_encoders.rs

1use std::{collections::HashSet, str::FromStr};
2
3use alloy::signers::local::PrivateKeySigner;
4use tycho_common::{models::Chain, Bytes};
5
6use crate::encoding::{
7    errors::EncodingError,
8    evm::{
9        approvals::permit2::Permit2,
10        constants::{GROUPABLE_PROTOCOLS, IN_TRANSFER_REQUIRED_PROTOCOLS},
11        encoding_utils::encode_tycho_router_call,
12        group_swaps::group_swaps,
13        strategy_encoder::strategy_encoders::{
14            SequentialSwapStrategyEncoder, SingleSwapStrategyEncoder, SplitSwapStrategyEncoder,
15        },
16        swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
17    },
18    models::{
19        EncodedSolution, EncodingContext, NativeAction, Solution, Transaction, TransferType,
20        UserTransferType,
21    },
22    strategy_encoder::StrategyEncoder,
23    tycho_encoder::TychoEncoder,
24};
25
26/// Encodes solutions to be used by the TychoRouter.
27///
28/// # Fields
29/// * `chain`: Chain to be used
30/// * `single_swap_strategy`: Encoder for single swaps
31/// * `sequential_swap_strategy`: Encoder for sequential swaps
32/// * `split_swap_strategy`: Encoder for split swaps
33/// * `router_address`: Address of the Tycho router contract
34/// * `user_transfer_type`: Type of user transfer
35/// * `permit2`: Optional Permit2 instance for permit transfers
36/// * `signer`: Optional signer (used only for permit2 and full calldata encoding)
37#[derive(Clone)]
38pub struct TychoRouterEncoder {
39    chain: Chain,
40    single_swap_strategy: SingleSwapStrategyEncoder,
41    sequential_swap_strategy: SequentialSwapStrategyEncoder,
42    split_swap_strategy: SplitSwapStrategyEncoder,
43    router_address: Bytes,
44    user_transfer_type: UserTransferType,
45    permit2: Option<Permit2>,
46    signer: Option<PrivateKeySigner>,
47}
48
49impl TychoRouterEncoder {
50    pub fn new(
51        chain: Chain,
52        swap_encoder_registry: SwapEncoderRegistry,
53        router_address: Bytes,
54        user_transfer_type: UserTransferType,
55        signer: Option<PrivateKeySigner>,
56    ) -> Result<Self, EncodingError> {
57        let permit2 = if user_transfer_type == UserTransferType::TransferFromPermit2 {
58            Some(Permit2::new()?)
59        } else {
60            None
61        };
62        Ok(TychoRouterEncoder {
63            single_swap_strategy: SingleSwapStrategyEncoder::new(
64                chain,
65                swap_encoder_registry.clone(),
66                user_transfer_type.clone(),
67                router_address.clone(),
68            )?,
69            sequential_swap_strategy: SequentialSwapStrategyEncoder::new(
70                chain,
71                swap_encoder_registry.clone(),
72                user_transfer_type.clone(),
73                router_address.clone(),
74            )?,
75            split_swap_strategy: SplitSwapStrategyEncoder::new(
76                chain,
77                swap_encoder_registry,
78                user_transfer_type.clone(),
79                router_address.clone(),
80            )?,
81            router_address,
82            permit2,
83            signer,
84            chain,
85            user_transfer_type,
86        })
87    }
88
89    fn encode_solution(&self, solution: &Solution) -> Result<EncodedSolution, EncodingError> {
90        self.validate_solution(solution)?;
91        let protocols: HashSet<String> = solution
92            .swaps
93            .iter()
94            .map(|swap| swap.component.protocol_system.clone())
95            .collect();
96
97        let mut encoded_solution = if (solution.swaps.len() == 1) ||
98            ((protocols.len() == 1 &&
99                protocols
100                    .iter()
101                    .any(|p| GROUPABLE_PROTOCOLS.contains(&p.as_str()))) &&
102                solution
103                    .swaps
104                    .iter()
105                    .all(|swap| swap.split == 0.0))
106        {
107            self.single_swap_strategy
108                .encode_strategy(solution)?
109        } else if solution
110            .swaps
111            .iter()
112            .all(|swap| swap.split == 0.0)
113        {
114            self.sequential_swap_strategy
115                .encode_strategy(solution)?
116        } else {
117            self.split_swap_strategy
118                .encode_strategy(solution)?
119        };
120
121        if let Some(permit2) = &self.permit2 {
122            let permit = permit2.get_permit(
123                &self.router_address,
124                &solution.sender,
125                &solution.given_token,
126                &solution.given_amount,
127            )?;
128            encoded_solution.permit = Some(permit);
129        }
130        Ok(encoded_solution)
131    }
132}
133
134impl TychoEncoder for TychoRouterEncoder {
135    fn encode_solutions(
136        &self,
137        solutions: Vec<Solution>,
138    ) -> Result<Vec<EncodedSolution>, EncodingError> {
139        let mut result: Vec<EncodedSolution> = Vec::new();
140        for solution in solutions.iter() {
141            let encoded_solution = self.encode_solution(solution)?;
142            result.push(encoded_solution);
143        }
144        Ok(result)
145    }
146
147    fn encode_full_calldata(
148        &self,
149        solutions: Vec<Solution>,
150    ) -> Result<Vec<Transaction>, EncodingError> {
151        let mut transactions: Vec<Transaction> = Vec::new();
152        for solution in solutions.iter() {
153            let encoded_solution = self.encode_solution(solution)?;
154
155            let transaction = encode_tycho_router_call(
156                self.chain.id(),
157                encoded_solution,
158                solution,
159                &self.user_transfer_type,
160                &self.chain.native_token().address,
161                self.signer.clone(),
162            )?;
163
164            transactions.push(transaction);
165        }
166        Ok(transactions)
167    }
168
169    /// Raises an `EncodingError` if the solution is not considered valid.
170    ///
171    /// A solution is considered valid if all the following conditions are met:
172    /// * The solution is not exact out.
173    /// * The solution has at least one swap.
174    /// * If the solution is wrapping, the given token is the chain's native token and the first
175    ///   swap's input is the chain's wrapped token.
176    /// * If the solution is unwrapping, the checked token is the chain's native token and the last
177    ///   swap's output is the chain's wrapped token.
178    /// * The token cannot appear more than once in the solution unless it is the first and last
179    ///   token (i.e. a true cyclical swap).
180    fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError> {
181        if solution.exact_out {
182            return Err(EncodingError::FatalError(
183                "Currently only exact input solutions are supported".to_string(),
184            ));
185        }
186        if solution.swaps.is_empty() {
187            return Err(EncodingError::FatalError("No swaps found in solution".to_string()));
188        }
189        let native_address = self.chain.native_token().address;
190        let wrapped_address = self
191            .chain
192            .wrapped_native_token()
193            .address;
194        if let Some(native_action) = &solution.native_action {
195            if native_action == &NativeAction::Wrap {
196                if solution.given_token != native_address {
197                    return Err(EncodingError::FatalError(
198                        "Native token must be the input token in order to wrap".to_string(),
199                    ));
200                }
201                if let Some(first_swap) = solution.swaps.first() {
202                    if first_swap.token_in != wrapped_address {
203                        return Err(EncodingError::FatalError(
204                            "Wrapped token must be the first swap's input in order to wrap"
205                                .to_string(),
206                        ));
207                    }
208                }
209            } else if native_action == &NativeAction::Unwrap {
210                if solution.checked_token != native_address {
211                    return Err(EncodingError::FatalError(
212                        "Native token must be the output token in order to unwrap".to_string(),
213                    ));
214                }
215                if let Some(last_swap) = solution.swaps.last() {
216                    if last_swap.token_out != wrapped_address {
217                        return Err(EncodingError::FatalError(
218                            "Wrapped token must be the last swap's output in order to unwrap"
219                                .to_string(),
220                        ));
221                    }
222                }
223            }
224        }
225
226        let mut solution_tokens = vec![];
227        let mut split_tokens_already_considered = HashSet::new();
228        for (i, swap) in solution.swaps.iter().enumerate() {
229            // so we don't count the split tokens more than once
230            if swap.split != 0.0 {
231                if !split_tokens_already_considered.contains(&swap.token_in) {
232                    solution_tokens.push(&swap.token_in);
233                    split_tokens_already_considered.insert(&swap.token_in);
234                }
235            } else {
236                // it might be the last swap of the split or a regular swap
237                if !split_tokens_already_considered.contains(&swap.token_in) {
238                    solution_tokens.push(&swap.token_in);
239                }
240            }
241            if i == solution.swaps.len() - 1 {
242                solution_tokens.push(&swap.token_out);
243            }
244        }
245
246        if solution_tokens.len() !=
247            solution_tokens
248                .iter()
249                .cloned()
250                .collect::<HashSet<&Bytes>>()
251                .len()
252        {
253            if let Some(last_swap) = solution.swaps.last() {
254                if solution.swaps[0].token_in != last_swap.token_out {
255                    return Err(EncodingError::FatalError(
256                        "Cyclical swaps are only allowed if they are the first and last token of a solution".to_string(),
257                    ));
258                } else {
259                    // it is a valid cyclical swap
260                    // we don't support any wrapping or unwrapping in this case
261                    if let Some(_native_action) = &solution.native_action {
262                        return Err(EncodingError::FatalError(
263                            "Wrapping/Unwrapping is not available in cyclical swaps".to_string(),
264                        ));
265                    }
266                }
267            }
268        }
269        Ok(())
270    }
271}
272
273/// Represents an encoder for one swap to be executed directly against an Executor.
274///
275/// This is useful when you want to bypass the Tycho Router, use your own Router contract and
276/// just need the calldata for a particular swap.
277///
278/// # Fields
279/// * `swap_encoder_registry`: Registry of swap encoders
280#[derive(Clone)]
281pub struct TychoExecutorEncoder {
282    swap_encoder_registry: SwapEncoderRegistry,
283}
284
285impl TychoExecutorEncoder {
286    pub fn new(swap_encoder_registry: SwapEncoderRegistry) -> Result<Self, EncodingError> {
287        Ok(TychoExecutorEncoder { swap_encoder_registry })
288    }
289
290    fn encode_executor_calldata(
291        &self,
292        solution: &Solution,
293    ) -> Result<EncodedSolution, EncodingError> {
294        let grouped_swaps = group_swaps(&solution.swaps);
295        let number_of_groups = grouped_swaps.len();
296        if number_of_groups > 1 {
297            return Err(EncodingError::InvalidInput(format!(
298                "Tycho executor encoder only supports one swap. Found {number_of_groups}"
299            )))
300        }
301
302        let grouped_swap = grouped_swaps
303            .first()
304            .ok_or_else(|| EncodingError::FatalError("Swap grouping failed".to_string()))?;
305
306        let swap_encoder = self
307            .swap_encoder_registry
308            .get_encoder(&grouped_swap.protocol_system)
309            .ok_or_else(|| {
310                EncodingError::InvalidInput(format!(
311                    "Swap encoder not found for protocol: {}",
312                    grouped_swap.protocol_system
313                ))
314            })?;
315
316        let mut grouped_protocol_data: Vec<u8> = vec![];
317        for swap in grouped_swap.swaps.iter() {
318            let transfer = if IN_TRANSFER_REQUIRED_PROTOCOLS
319                .contains(&swap.component.protocol_system.as_str())
320            {
321                TransferType::Transfer
322            } else {
323                TransferType::None
324            };
325            let encoding_context = EncodingContext {
326                receiver: solution.receiver.clone(),
327                exact_out: solution.exact_out,
328                router_address: None,
329                group_token_in: grouped_swap.token_in.clone(),
330                group_token_out: grouped_swap.token_out.clone(),
331                transfer_type: transfer,
332            };
333            let protocol_data = swap_encoder.encode_swap(swap, &encoding_context)?;
334            grouped_protocol_data.extend(protocol_data);
335        }
336
337        let executor_address = Bytes::from_str(swap_encoder.executor_address())
338            .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;
339
340        Ok(EncodedSolution {
341            swaps: grouped_protocol_data,
342            interacting_with: executor_address,
343            permit: None,
344            function_signature: "".to_string(),
345            n_tokens: 0,
346        })
347    }
348}
349
350impl TychoEncoder for TychoExecutorEncoder {
351    fn encode_solutions(
352        &self,
353        solutions: Vec<Solution>,
354    ) -> Result<Vec<EncodedSolution>, EncodingError> {
355        let solution = solutions
356            .first()
357            .ok_or(EncodingError::FatalError("No solutions found".to_string()))?;
358        self.validate_solution(solution)?;
359
360        let encoded_solution = self.encode_executor_calldata(solution)?;
361
362        Ok(vec![encoded_solution])
363    }
364
365    fn encode_full_calldata(
366        &self,
367        _solutions: Vec<Solution>,
368    ) -> Result<Vec<Transaction>, EncodingError> {
369        Err(EncodingError::NotImplementedError(
370            "Full calldata encoding is not supported for TychoExecutorEncoder".to_string(),
371        ))
372    }
373
374    /// Raises an `EncodingError` if the solution is not considered valid.
375    ///
376    /// A solution is considered valid if all the following conditions are met:
377    /// * The solution is not exact out.
378    fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError> {
379        if solution.exact_out {
380            return Err(EncodingError::FatalError(
381                "Currently only exact input solutions are supported".to_string(),
382            ));
383        }
384        Ok(())
385    }
386}
387
388#[cfg(test)]
389mod tests {
390    use std::{collections::HashMap, str::FromStr};
391
392    use num_bigint::{BigInt, BigUint};
393    use tycho_common::models::{protocol::ProtocolComponent, Chain};
394
395    use super::*;
396    use crate::encoding::models::Swap;
397
398    fn dai() -> Bytes {
399        Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap()
400    }
401
402    fn eth() -> Bytes {
403        Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap()
404    }
405
406    fn weth() -> Bytes {
407        Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap()
408    }
409
410    fn usdc() -> Bytes {
411        Bytes::from_str("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48").unwrap()
412    }
413
414    fn wbtc() -> Bytes {
415        Bytes::from_str("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599").unwrap()
416    }
417
418    fn pepe() -> Bytes {
419        Bytes::from_str("0x6982508145454Ce325dDbE47a25d4ec3d2311933").unwrap()
420    }
421
422    // Fee and tick spacing information for this test is obtained by querying the
423    // USV4 Position Manager contract: 0xbd216513d74c8cf14cf4747e6aaa6420ff64ee9e
424    // Using the poolKeys function with the first 25 bytes of the pool id
425    fn swap_usdc_eth_univ4() -> Swap<'static> {
426        let pool_fee_usdc_eth = Bytes::from(BigInt::from(3000).to_signed_bytes_be());
427        let tick_spacing_usdc_eth = Bytes::from(BigInt::from(60).to_signed_bytes_be());
428        let mut static_attributes_usdc_eth: HashMap<String, Bytes> = HashMap::new();
429        static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth);
430        static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth);
431        Swap {
432            component: ProtocolComponent {
433                id: "0xdce6394339af00981949f5f3baf27e3610c76326a700af57e4b3e3ae4977f78d"
434                    .to_string(),
435                protocol_system: "uniswap_v4".to_string(),
436                static_attributes: static_attributes_usdc_eth,
437                ..Default::default()
438            },
439            token_in: usdc().clone(),
440            token_out: eth().clone(),
441            split: 0f64,
442            user_data: None,
443            protocol_state: None,
444        }
445    }
446
447    fn swap_eth_pepe_univ4() -> Swap<'static> {
448        let pool_fee_eth_pepe = Bytes::from(BigInt::from(25000).to_signed_bytes_be());
449        let tick_spacing_eth_pepe = Bytes::from(BigInt::from(500).to_signed_bytes_be());
450        let mut static_attributes_eth_pepe: HashMap<String, Bytes> = HashMap::new();
451        static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe);
452        static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe);
453        Swap {
454            component: ProtocolComponent {
455                id: "0xecd73ecbf77219f21f129c8836d5d686bbc27d264742ddad620500e3e548e2c9"
456                    .to_string(),
457                protocol_system: "uniswap_v4".to_string(),
458                static_attributes: static_attributes_eth_pepe,
459                ..Default::default()
460            },
461            token_in: eth().clone(),
462            token_out: pepe().clone(),
463            split: 0f64,
464            user_data: None,
465            protocol_state: None,
466        }
467    }
468
469    fn router_address() -> Bytes {
470        Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap()
471    }
472
473    fn eth_chain() -> Chain {
474        Chain::Ethereum
475    }
476
477    fn get_swap_encoder_registry() -> SwapEncoderRegistry {
478        SwapEncoderRegistry::new(
479            Some("config/test_executor_addresses.json".to_string()),
480            eth_chain(),
481        )
482        .unwrap()
483    }
484
485    fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> TychoRouterEncoder {
486        TychoRouterEncoder::new(
487            eth_chain(),
488            get_swap_encoder_registry(),
489            router_address(),
490            user_transfer_type,
491            None,
492        )
493        .unwrap()
494    }
495
496    mod router_encoder {
497        use super::*;
498
499        #[test]
500        #[allow(deprecated)]
501        fn test_encode_router_calldata_single_swap() {
502            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
503            let eth_amount_in = BigUint::from(1000u32);
504            let swap = Swap {
505                component: ProtocolComponent {
506                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
507                    protocol_system: "uniswap_v2".to_string(),
508                    ..Default::default()
509                },
510                token_in: weth(),
511                token_out: dai(),
512                split: 0f64,
513                user_data: None,
514                protocol_state: None,
515            };
516
517            let solution = Solution {
518                exact_out: false,
519                given_amount: eth_amount_in.clone(),
520                given_token: eth(),
521                checked_token: dai(),
522                swaps: vec![swap],
523                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
524                native_action: Some(NativeAction::Wrap),
525                ..Default::default()
526            };
527
528            let transactions = encoder.encode_full_calldata(vec![solution]);
529            assert!(transactions.is_ok());
530            let transactions = transactions.unwrap();
531            assert_eq!(transactions.len(), 1);
532            assert_eq!(transactions[0].value, eth_amount_in);
533            assert_eq!(
534                transactions[0].to,
535                Bytes::from_str("0x3ede3eca2a72b3aecc820e955b36f38437d01395").unwrap()
536            );
537            // single swap selector
538            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "5c4b639c");
539        }
540
541        #[test]
542        #[allow(deprecated)]
543        fn test_encode_router_calldata_single_swap_group() {
544            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
545            let solution = Solution {
546                exact_out: false,
547                given_token: usdc(),
548                given_amount: BigUint::from_str("1000_000000").unwrap(),
549                checked_token: pepe(),
550                checked_amount: BigUint::from_str("105_152_000000000000000000").unwrap(),
551                sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
552                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
553                swaps: vec![swap_usdc_eth_univ4(), swap_eth_pepe_univ4()],
554                ..Default::default()
555            };
556
557            let transactions = encoder.encode_full_calldata(vec![solution]);
558            assert!(transactions.is_ok());
559            let transactions = transactions.unwrap();
560            assert_eq!(transactions.len(), 1);
561            // single swap selector
562            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "5c4b639c");
563        }
564
565        #[test]
566        #[allow(deprecated)]
567        fn test_encode_router_calldata_sequential_swap() {
568            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
569            let eth_amount_in = BigUint::from(1000u32);
570            let swap_weth_dai = Swap {
571                component: ProtocolComponent {
572                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
573                    protocol_system: "uniswap_v2".to_string(),
574                    ..Default::default()
575                },
576                token_in: weth(),
577                token_out: dai(),
578                split: 0f64,
579                user_data: None,
580                protocol_state: None,
581            };
582
583            let swap_dai_usdc = Swap {
584                component: ProtocolComponent {
585                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
586                    protocol_system: "uniswap_v2".to_string(),
587                    ..Default::default()
588                },
589                token_in: dai(),
590                token_out: usdc(),
591                split: 0f64,
592                user_data: None,
593                protocol_state: None,
594            };
595
596            let solution = Solution {
597                exact_out: false,
598                given_amount: eth_amount_in.clone(),
599                given_token: eth(),
600                checked_token: usdc(),
601                swaps: vec![swap_weth_dai, swap_dai_usdc],
602                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
603                native_action: Some(NativeAction::Wrap),
604                checked_amount: BigUint::from(1000u32),
605                ..Default::default()
606            };
607
608            let transactions = encoder.encode_full_calldata(vec![solution]);
609            assert!(transactions.is_ok());
610            let transactions = transactions.unwrap();
611            assert_eq!(transactions.len(), 1);
612            assert_eq!(transactions[0].value, eth_amount_in);
613            // sequential swap selector
614            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "e21dd0d3");
615        }
616
617        #[test]
618        fn test_encode_router_calldata_split_swap_group() {
619            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
620            let mut swap_usdc_eth = swap_usdc_eth_univ4();
621            swap_usdc_eth.split = 0.5; // Set split to 50%
622            let solution = Solution {
623                exact_out: false,
624                given_token: usdc(),
625                given_amount: BigUint::from_str("1000_000000").unwrap(),
626                checked_token: eth(),
627                checked_amount: BigUint::from_str("105_152_000000000000000000").unwrap(),
628                sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
629                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
630                swaps: vec![swap_usdc_eth, swap_usdc_eth_univ4()],
631                ..Default::default()
632            };
633
634            let encoded_solution_res = encoder.encode_solution(&solution);
635            assert!(encoded_solution_res.is_ok());
636
637            let encoded_solution = encoded_solution_res.unwrap();
638            assert!(encoded_solution
639                .function_signature
640                .contains("splitSwap"));
641        }
642
643        #[test]
644        fn test_validate_fails_for_exact_out() {
645            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
646            let solution = Solution {
647                exact_out: true, // This should cause an error
648                ..Default::default()
649            };
650            let result = encoder.validate_solution(&solution);
651
652            assert!(result.is_err());
653            assert_eq!(
654                result.err().unwrap(),
655                EncodingError::FatalError(
656                    "Currently only exact input solutions are supported".to_string()
657                )
658            );
659        }
660
661        #[test]
662        fn test_validate_passes_for_wrap() {
663            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
664            let swap = Swap {
665                component: ProtocolComponent {
666                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
667                    protocol_system: "uniswap_v2".to_string(),
668                    ..Default::default()
669                },
670                token_in: weth(),
671                token_out: dai(),
672                split: 0f64,
673                user_data: None,
674                protocol_state: None,
675            };
676
677            let solution = Solution {
678                exact_out: false,
679                given_token: eth(),
680                checked_token: dai(),
681                swaps: vec![swap],
682                native_action: Some(NativeAction::Wrap),
683                ..Default::default()
684            };
685
686            let result = encoder.validate_solution(&solution);
687
688            assert!(result.is_ok());
689        }
690
691        #[test]
692        fn test_validate_fails_for_wrap_wrong_input() {
693            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
694            let swap = Swap {
695                component: ProtocolComponent {
696                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
697                    protocol_system: "uniswap_v2".to_string(),
698                    ..Default::default()
699                },
700                token_in: weth(),
701                token_out: dai(),
702                split: 0f64,
703                user_data: None,
704                protocol_state: None,
705            };
706
707            let solution = Solution {
708                exact_out: false,
709                given_token: weth(),
710                swaps: vec![swap],
711                native_action: Some(NativeAction::Wrap),
712                ..Default::default()
713            };
714
715            let result = encoder.validate_solution(&solution);
716
717            assert!(result.is_err());
718            assert_eq!(
719                result.err().unwrap(),
720                EncodingError::FatalError(
721                    "Native token must be the input token in order to wrap".to_string()
722                )
723            );
724        }
725
726        #[test]
727        fn test_validate_fails_for_wrap_wrong_first_swap() {
728            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
729            let swap = Swap {
730                component: ProtocolComponent {
731                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
732                    protocol_system: "uniswap_v2".to_string(),
733                    ..Default::default()
734                },
735                token_in: eth(),
736                token_out: dai(),
737                split: 0f64,
738                user_data: None,
739                protocol_state: None,
740            };
741
742            let solution = Solution {
743                exact_out: false,
744                given_token: eth(),
745                swaps: vec![swap],
746                native_action: Some(NativeAction::Wrap),
747                ..Default::default()
748            };
749
750            let result = encoder.validate_solution(&solution);
751
752            assert!(result.is_err());
753            assert_eq!(
754                result.err().unwrap(),
755                EncodingError::FatalError(
756                    "Wrapped token must be the first swap's input in order to wrap".to_string()
757                )
758            );
759        }
760
761        #[test]
762        fn test_validate_fails_no_swaps() {
763            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
764            let solution = Solution {
765                exact_out: false,
766                given_token: eth(),
767                swaps: vec![],
768                native_action: Some(NativeAction::Wrap),
769                ..Default::default()
770            };
771
772            let result = encoder.validate_solution(&solution);
773
774            assert!(result.is_err());
775            assert_eq!(
776                result.err().unwrap(),
777                EncodingError::FatalError("No swaps found in solution".to_string())
778            );
779        }
780
781        #[test]
782        fn test_validate_passes_for_unwrap() {
783            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
784            let swap = Swap {
785                component: ProtocolComponent {
786                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
787                    protocol_system: "uniswap_v2".to_string(),
788                    ..Default::default()
789                },
790                token_in: dai(),
791                token_out: weth(),
792                split: 0f64,
793                user_data: None,
794                protocol_state: None,
795            };
796
797            let solution = Solution {
798                exact_out: false,
799                checked_token: eth(),
800                swaps: vec![swap],
801                native_action: Some(NativeAction::Unwrap),
802                ..Default::default()
803            };
804
805            let result = encoder.validate_solution(&solution);
806
807            assert!(result.is_ok());
808        }
809
810        #[test]
811        fn test_validate_fails_for_unwrap_wrong_output() {
812            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
813            let swap = Swap {
814                component: ProtocolComponent {
815                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
816                    protocol_system: "uniswap_v2".to_string(),
817                    ..Default::default()
818                },
819                token_in: dai(),
820                token_out: weth(),
821                split: 0f64,
822                user_data: None,
823                protocol_state: None,
824            };
825
826            let solution = Solution {
827                exact_out: false,
828                given_token: dai(),
829                checked_token: weth(),
830                swaps: vec![swap],
831                native_action: Some(NativeAction::Unwrap),
832                ..Default::default()
833            };
834
835            let result = encoder.validate_solution(&solution);
836
837            assert!(result.is_err());
838            assert_eq!(
839                result.err().unwrap(),
840                EncodingError::FatalError(
841                    "Native token must be the output token in order to unwrap".to_string()
842                )
843            );
844        }
845
846        #[test]
847        fn test_validate_fails_for_unwrap_wrong_last_swap() {
848            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
849            let swap = Swap {
850                component: ProtocolComponent {
851                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
852                    protocol_system: "uniswap_v2".to_string(),
853                    ..Default::default()
854                },
855                token_in: dai(),
856                token_out: eth(),
857                split: 0f64,
858                user_data: None,
859                protocol_state: None,
860            };
861
862            let solution = Solution {
863                exact_out: false,
864                checked_token: eth(),
865                swaps: vec![swap],
866                native_action: Some(NativeAction::Unwrap),
867                ..Default::default()
868            };
869
870            let result = encoder.validate_solution(&solution);
871
872            assert!(result.is_err());
873            assert_eq!(
874                result.err().unwrap(),
875                EncodingError::FatalError(
876                    "Wrapped token must be the last swap's output in order to unwrap".to_string()
877                )
878            );
879        }
880
881        #[test]
882        fn test_validate_cyclical_swap() {
883            // This validation passes because the cyclical swap is the first and last token
884            //      50% ->  WETH
885            // DAI -              -> DAI
886            //      50% -> WETH
887            // (some of the pool addresses in this test are fake)
888            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
889            let swaps = vec![
890                Swap {
891                    component: ProtocolComponent {
892                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
893                        protocol_system: "uniswap_v2".to_string(),
894                        ..Default::default()
895                    },
896                    token_in: dai(),
897                    token_out: weth(),
898                    split: 0.5f64,
899                    user_data: None,
900                    protocol_state: None,
901                },
902                Swap {
903                    component: ProtocolComponent {
904                        id: "0x0000000000000000000000000000000000000000".to_string(),
905                        protocol_system: "uniswap_v2".to_string(),
906                        ..Default::default()
907                    },
908                    token_in: dai(),
909                    token_out: weth(),
910                    split: 0f64,
911                    user_data: None,
912                    protocol_state: None,
913                },
914                Swap {
915                    component: ProtocolComponent {
916                        id: "0x0000000000000000000000000000000000000000".to_string(),
917                        protocol_system: "uniswap_v2".to_string(),
918                        ..Default::default()
919                    },
920                    token_in: weth(),
921                    token_out: dai(),
922                    split: 0f64,
923                    user_data: None,
924                    protocol_state: None,
925                },
926            ];
927
928            let solution = Solution {
929                exact_out: false,
930                given_token: dai(),
931                checked_token: dai(),
932                swaps,
933                ..Default::default()
934            };
935
936            let result = encoder.validate_solution(&solution);
937
938            assert!(result.is_ok());
939        }
940
941        #[test]
942        fn test_validate_cyclical_swap_fail() {
943            // This test should fail because the cyclical swap is not the first and last token
944            // DAI -> WETH -> USDC -> DAI -> WBTC
945            // (some of the pool addresses in this test are fake)
946            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
947            let swaps = vec![
948                Swap {
949                    component: ProtocolComponent {
950                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
951                        protocol_system: "uniswap_v2".to_string(),
952                        ..Default::default()
953                    },
954                    token_in: dai(),
955                    token_out: weth(),
956                    split: 0f64,
957                    user_data: None,
958                    protocol_state: None,
959                },
960                Swap {
961                    component: ProtocolComponent {
962                        id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(),
963                        protocol_system: "uniswap_v2".to_string(),
964                        ..Default::default()
965                    },
966                    token_in: weth(),
967                    token_out: usdc(),
968                    split: 0f64,
969                    user_data: None,
970                    protocol_state: None,
971                },
972                Swap {
973                    component: ProtocolComponent {
974                        id: "0x0000000000000000000000000000000000000000".to_string(),
975                        protocol_system: "uniswap_v2".to_string(),
976                        ..Default::default()
977                    },
978                    token_in: usdc(),
979                    token_out: dai(),
980                    split: 0f64,
981                    user_data: None,
982                    protocol_state: None,
983                },
984                Swap {
985                    component: ProtocolComponent {
986                        id: "0x0000000000000000000000000000000000000000".to_string(),
987                        protocol_system: "uniswap_v2".to_string(),
988                        ..Default::default()
989                    },
990                    token_in: dai(),
991                    token_out: wbtc(),
992                    split: 0f64,
993                    user_data: None,
994                    protocol_state: None,
995                },
996            ];
997
998            let solution = Solution {
999                exact_out: false,
1000                given_token: dai(),
1001                checked_token: wbtc(),
1002                swaps,
1003                ..Default::default()
1004            };
1005
1006            let result = encoder.validate_solution(&solution);
1007
1008            assert!(result.is_err());
1009            assert_eq!(
1010            result.err().unwrap(),
1011            EncodingError::FatalError(
1012                "Cyclical swaps are only allowed if they are the first and last token of a solution".to_string()
1013            )
1014        );
1015        }
1016        #[test]
1017        fn test_validate_cyclical_swap_split_output() {
1018            // This validation passes because it is a valid cyclical swap
1019            //             -> WETH
1020            // WETH -> DAI
1021            //             -> WETH
1022            // (some of the pool addresses in this test are fake)
1023            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
1024            let swaps = vec![
1025                Swap {
1026                    component: ProtocolComponent {
1027                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
1028                        protocol_system: "uniswap_v2".to_string(),
1029                        ..Default::default()
1030                    },
1031                    token_in: weth(),
1032                    token_out: dai(),
1033                    split: 0f64,
1034                    user_data: None,
1035                    protocol_state: None,
1036                },
1037                Swap {
1038                    component: ProtocolComponent {
1039                        id: "0x0000000000000000000000000000000000000000".to_string(),
1040                        protocol_system: "uniswap_v2".to_string(),
1041                        ..Default::default()
1042                    },
1043                    token_in: dai(),
1044                    token_out: weth(),
1045                    split: 0.5f64,
1046                    user_data: None,
1047                    protocol_state: None,
1048                },
1049                Swap {
1050                    component: ProtocolComponent {
1051                        id: "0x0000000000000000000000000000000000000000".to_string(),
1052                        protocol_system: "uniswap_v2".to_string(),
1053                        ..Default::default()
1054                    },
1055                    token_in: dai(),
1056                    token_out: weth(),
1057                    split: 0f64,
1058                    user_data: None,
1059                    protocol_state: None,
1060                },
1061            ];
1062
1063            let solution = Solution {
1064                exact_out: false,
1065                given_token: weth(),
1066                checked_token: weth(),
1067                swaps,
1068                ..Default::default()
1069            };
1070
1071            let result = encoder.validate_solution(&solution);
1072
1073            assert!(result.is_ok());
1074        }
1075
1076        #[test]
1077        fn test_validate_cyclical_swap_native_action_fail() {
1078            // This validation fails because there is a native action with a valid cyclical swap
1079            // ETH -> WETH -> DAI -> WETH
1080            // (some of the pool addresses in this test are fake)
1081            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
1082            let swaps = vec![
1083                Swap {
1084                    component: ProtocolComponent {
1085                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
1086                        protocol_system: "uniswap_v2".to_string(),
1087                        ..Default::default()
1088                    },
1089                    token_in: weth(),
1090                    token_out: dai(),
1091                    split: 0f64,
1092                    user_data: None,
1093                    protocol_state: None,
1094                },
1095                Swap {
1096                    component: ProtocolComponent {
1097                        id: "0x0000000000000000000000000000000000000000".to_string(),
1098                        protocol_system: "uniswap_v2".to_string(),
1099                        ..Default::default()
1100                    },
1101                    token_in: dai(),
1102                    token_out: weth(),
1103                    split: 0f64,
1104                    user_data: None,
1105                    protocol_state: None,
1106                },
1107            ];
1108
1109            let solution = Solution {
1110                exact_out: false,
1111                given_token: eth(),
1112                checked_token: weth(),
1113                swaps,
1114                native_action: Some(NativeAction::Wrap),
1115                ..Default::default()
1116            };
1117
1118            let result = encoder.validate_solution(&solution);
1119
1120            assert!(result.is_err());
1121            assert_eq!(
1122                result.err().unwrap(),
1123                EncodingError::FatalError(
1124                    "Wrapping/Unwrapping is not available in cyclical swaps"
1125                        .to_string()
1126                        .to_string()
1127                )
1128            );
1129        }
1130    }
1131
1132    mod executor_encoder {
1133        use std::str::FromStr;
1134
1135        use alloy::hex::encode;
1136        use num_bigint::BigUint;
1137        use tycho_common::{models::protocol::ProtocolComponent, Bytes};
1138
1139        use super::*;
1140        use crate::encoding::models::{Solution, Swap};
1141
1142        #[test]
1143        fn test_executor_encoder_encode() {
1144            let swap_encoder_registry = get_swap_encoder_registry();
1145            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();
1146
1147            let token_in = weth();
1148            let token_out = dai();
1149
1150            let swap = Swap {
1151                component: ProtocolComponent {
1152                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
1153                    protocol_system: "uniswap_v2".to_string(),
1154                    ..Default::default()
1155                },
1156                token_in: token_in.clone(),
1157                token_out: token_out.clone(),
1158                split: 0f64,
1159                user_data: None,
1160                protocol_state: None,
1161            };
1162
1163            let solution = Solution {
1164                exact_out: false,
1165                given_token: token_in,
1166                given_amount: BigUint::from(1000000000000000000u64),
1167                checked_token: token_out,
1168                checked_amount: BigUint::from(1000000000000000000u64),
1169                sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
1170                // The receiver was generated with `makeAddr("bob") using forge`
1171                receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
1172                swaps: vec![swap],
1173                native_action: None,
1174            };
1175
1176            let encoded_solutions = encoder
1177                .encode_solutions(vec![solution])
1178                .unwrap();
1179            let encoded = encoded_solutions
1180                .first()
1181                .expect("Expected at least one encoded solution");
1182            let hex_protocol_data = encode(&encoded.swaps);
1183            assert_eq!(
1184                encoded.interacting_with,
1185                Bytes::from_str("0x5615deb798bb3e4dfa0139dfa1b3d433cc23b72f").unwrap()
1186            );
1187            assert_eq!(
1188                hex_protocol_data,
1189                String::from(concat!(
1190                    // in token
1191                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
1192                    // component id
1193                    "a478c2975ab1ea89e8196811f51a7b7ade33eb11",
1194                    // receiver
1195                    "1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e",
1196                    // zero for one
1197                    "00",
1198                    // transfer true
1199                    "01",
1200                ))
1201            );
1202        }
1203
1204        #[test]
1205        fn test_executor_encoder_too_many_swaps() {
1206            let swap_encoder_registry = get_swap_encoder_registry();
1207            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();
1208
1209            let token_in = weth();
1210            let token_out = dai();
1211
1212            let swap = Swap {
1213                component: ProtocolComponent {
1214                    protocol_system: "uniswap_v2".to_string(),
1215                    ..Default::default()
1216                },
1217                token_in: token_in.clone(),
1218                token_out: token_out.clone(),
1219                split: 0f64,
1220                user_data: None,
1221                protocol_state: None,
1222            };
1223
1224            let solution = Solution {
1225                exact_out: false,
1226                given_token: token_in,
1227                given_amount: BigUint::from(1000000000000000000u64),
1228                checked_token: token_out,
1229                checked_amount: BigUint::from(1000000000000000000u64),
1230                sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
1231                receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
1232                swaps: vec![swap.clone(), swap],
1233                native_action: None,
1234            };
1235
1236            let result = encoder.encode_solutions(vec![solution]);
1237            assert!(result.is_err());
1238        }
1239
1240        #[test]
1241        fn test_executor_encoder_grouped_swaps() {
1242            let swap_encoder_registry = get_swap_encoder_registry();
1243            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();
1244
1245            let usdc = usdc();
1246            let pepe = pepe();
1247
1248            let solution = Solution {
1249                exact_out: false,
1250                given_token: usdc,
1251                given_amount: BigUint::from_str("1000_000000").unwrap(),
1252                checked_token: pepe,
1253                checked_amount: BigUint::from(1000000000000000000u64),
1254                sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
1255                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
1256                swaps: vec![swap_usdc_eth_univ4(), swap_eth_pepe_univ4()],
1257                ..Default::default()
1258            };
1259
1260            let encoded_solutions = encoder
1261                .encode_solutions(vec![solution])
1262                .unwrap();
1263            let encoded_solution = encoded_solutions
1264                .first()
1265                .expect("Expected at least one encoded solution");
1266            let hex_protocol_data = encode(&encoded_solution.swaps);
1267            assert_eq!(
1268                encoded_solution.interacting_with,
1269                Bytes::from_str("0xf62849f9a0b5bf2913b396098f7c7019b51a820a").unwrap()
1270            );
1271            assert_eq!(
1272                hex_protocol_data,
1273                String::from(concat!(
1274                    // group in token
1275                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
1276                    // group out token
1277                    "6982508145454ce325ddbe47a25d4ec3d2311933",
1278                    // zero for one
1279                    "00",
1280                    // transfer type Transfer
1281                    "01",
1282                    // receiver
1283                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2",
1284                    // first pool intermediary token (ETH)
1285                    "0000000000000000000000000000000000000000",
1286                    // fee
1287                    "000bb8",
1288                    // tick spacing
1289                    "00003c",
1290                    // second pool intermediary token (PEPE)
1291                    "6982508145454ce325ddbe47a25d4ec3d2311933",
1292                    // fee
1293                    "0061a8",
1294                    // tick spacing
1295                    "0001f4"
1296                ))
1297            );
1298        }
1299    }
1300}