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, SwapBuilder};
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        SwapBuilder::new(
432            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            usdc().clone(),
440            eth().clone(),
441        )
442        .build()
443    }
444
445    fn swap_eth_pepe_univ4() -> Swap<'static> {
446        let pool_fee_eth_pepe = Bytes::from(BigInt::from(25000).to_signed_bytes_be());
447        let tick_spacing_eth_pepe = Bytes::from(BigInt::from(500).to_signed_bytes_be());
448        let mut static_attributes_eth_pepe: HashMap<String, Bytes> = HashMap::new();
449        static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe);
450        static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe);
451        SwapBuilder::new(
452            ProtocolComponent {
453                id: "0xecd73ecbf77219f21f129c8836d5d686bbc27d264742ddad620500e3e548e2c9"
454                    .to_string(),
455                protocol_system: "uniswap_v4".to_string(),
456                static_attributes: static_attributes_eth_pepe,
457                ..Default::default()
458            },
459            eth().clone(),
460            pepe().clone(),
461        )
462        .build()
463    }
464
465    fn router_address() -> Bytes {
466        Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap()
467    }
468
469    fn eth_chain() -> Chain {
470        Chain::Ethereum
471    }
472
473    fn get_swap_encoder_registry() -> SwapEncoderRegistry {
474        SwapEncoderRegistry::new(
475            Some("config/test_executor_addresses.json".to_string()),
476            eth_chain(),
477        )
478        .unwrap()
479    }
480
481    fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> TychoRouterEncoder {
482        TychoRouterEncoder::new(
483            eth_chain(),
484            get_swap_encoder_registry(),
485            router_address(),
486            user_transfer_type,
487            None,
488        )
489        .unwrap()
490    }
491
492    mod router_encoder {
493        use super::*;
494
495        #[test]
496        #[allow(deprecated)]
497        fn test_encode_router_calldata_single_swap() {
498            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
499            let eth_amount_in = BigUint::from(1000u32);
500            let swap = SwapBuilder::new(
501                ProtocolComponent {
502                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
503                    protocol_system: "uniswap_v2".to_string(),
504                    ..Default::default()
505                },
506                weth().clone(),
507                dai().clone(),
508            )
509            .build();
510
511            let solution = Solution {
512                exact_out: false,
513                given_amount: eth_amount_in.clone(),
514                given_token: eth(),
515                checked_token: dai(),
516                swaps: vec![swap],
517                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
518                native_action: Some(NativeAction::Wrap),
519                ..Default::default()
520            };
521
522            let transactions = encoder.encode_full_calldata(vec![solution]);
523            assert!(transactions.is_ok());
524            let transactions = transactions.unwrap();
525            assert_eq!(transactions.len(), 1);
526            assert_eq!(transactions[0].value, eth_amount_in);
527            assert_eq!(
528                transactions[0].to,
529                Bytes::from_str("0x3ede3eca2a72b3aecc820e955b36f38437d01395").unwrap()
530            );
531            // single swap selector
532            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "5c4b639c");
533        }
534
535        #[test]
536        #[allow(deprecated)]
537        fn test_encode_router_calldata_single_swap_group() {
538            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
539            let solution = Solution {
540                exact_out: false,
541                given_token: usdc(),
542                given_amount: BigUint::from_str("1000_000000").unwrap(),
543                checked_token: pepe(),
544                checked_amount: BigUint::from_str("105_152_000000000000000000").unwrap(),
545                sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
546                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
547                swaps: vec![swap_usdc_eth_univ4(), swap_eth_pepe_univ4()],
548                ..Default::default()
549            };
550
551            let transactions = encoder.encode_full_calldata(vec![solution]);
552            assert!(transactions.is_ok());
553            let transactions = transactions.unwrap();
554            assert_eq!(transactions.len(), 1);
555            // single swap selector
556            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "5c4b639c");
557        }
558
559        #[test]
560        #[allow(deprecated)]
561        fn test_encode_router_calldata_sequential_swap() {
562            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
563            let eth_amount_in = BigUint::from(1000u32);
564            let swap_weth_dai = SwapBuilder::new(
565                ProtocolComponent {
566                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
567                    protocol_system: "uniswap_v2".to_string(),
568                    ..Default::default()
569                },
570                weth().clone(),
571                dai().clone(),
572            )
573            .build();
574
575            let swap_dai_usdc = SwapBuilder::new(
576                ProtocolComponent {
577                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
578                    protocol_system: "uniswap_v2".to_string(),
579                    ..Default::default()
580                },
581                dai().clone(),
582                usdc().clone(),
583            )
584            .build();
585
586            let solution = Solution {
587                exact_out: false,
588                given_amount: eth_amount_in.clone(),
589                given_token: eth(),
590                checked_token: usdc(),
591                swaps: vec![swap_weth_dai, swap_dai_usdc],
592                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
593                native_action: Some(NativeAction::Wrap),
594                checked_amount: BigUint::from(1000u32),
595                ..Default::default()
596            };
597
598            let transactions = encoder.encode_full_calldata(vec![solution]);
599            assert!(transactions.is_ok());
600            let transactions = transactions.unwrap();
601            assert_eq!(transactions.len(), 1);
602            assert_eq!(transactions[0].value, eth_amount_in);
603            // sequential swap selector
604            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "e21dd0d3");
605        }
606
607        #[test]
608        fn test_encode_router_calldata_split_swap_group() {
609            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
610            let mut swap_usdc_eth = swap_usdc_eth_univ4();
611            swap_usdc_eth.split = 0.5; // Set split to 50%
612            let solution = Solution {
613                exact_out: false,
614                given_token: usdc(),
615                given_amount: BigUint::from_str("1000_000000").unwrap(),
616                checked_token: eth(),
617                checked_amount: BigUint::from_str("105_152_000000000000000000").unwrap(),
618                sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
619                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
620                swaps: vec![swap_usdc_eth, swap_usdc_eth_univ4()],
621                ..Default::default()
622            };
623
624            let encoded_solution_res = encoder.encode_solution(&solution);
625            assert!(encoded_solution_res.is_ok());
626
627            let encoded_solution = encoded_solution_res.unwrap();
628            assert!(encoded_solution
629                .function_signature
630                .contains("splitSwap"));
631        }
632
633        #[test]
634        fn test_validate_fails_for_exact_out() {
635            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
636            let solution = Solution {
637                exact_out: true, // This should cause an error
638                ..Default::default()
639            };
640            let result = encoder.validate_solution(&solution);
641
642            assert!(result.is_err());
643            assert_eq!(
644                result.err().unwrap(),
645                EncodingError::FatalError(
646                    "Currently only exact input solutions are supported".to_string()
647                )
648            );
649        }
650
651        #[test]
652        fn test_validate_passes_for_wrap() {
653            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
654            let swap = SwapBuilder::new(
655                ProtocolComponent {
656                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
657                    protocol_system: "uniswap_v2".to_string(),
658                    ..Default::default()
659                },
660                weth().clone(),
661                dai().clone(),
662            )
663            .build();
664
665            let solution = Solution {
666                exact_out: false,
667                given_token: eth(),
668                checked_token: dai(),
669                swaps: vec![swap],
670                native_action: Some(NativeAction::Wrap),
671                ..Default::default()
672            };
673
674            let result = encoder.validate_solution(&solution);
675
676            assert!(result.is_ok());
677        }
678
679        #[test]
680        fn test_validate_fails_for_wrap_wrong_input() {
681            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
682            let swap = SwapBuilder::new(
683                ProtocolComponent {
684                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
685                    protocol_system: "uniswap_v2".to_string(),
686                    ..Default::default()
687                },
688                weth().clone(),
689                dai().clone(),
690            )
691            .build();
692
693            let solution = Solution {
694                exact_out: false,
695                given_token: weth(),
696                swaps: vec![swap],
697                native_action: Some(NativeAction::Wrap),
698                ..Default::default()
699            };
700
701            let result = encoder.validate_solution(&solution);
702
703            assert!(result.is_err());
704            assert_eq!(
705                result.err().unwrap(),
706                EncodingError::FatalError(
707                    "Native token must be the input token in order to wrap".to_string()
708                )
709            );
710        }
711
712        #[test]
713        fn test_validate_fails_for_wrap_wrong_first_swap() {
714            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
715            let swap = SwapBuilder::new(
716                ProtocolComponent {
717                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
718                    protocol_system: "uniswap_v2".to_string(),
719                    ..Default::default()
720                },
721                eth().clone(),
722                dai().clone(),
723            )
724            .build();
725
726            let solution = Solution {
727                exact_out: false,
728                given_token: eth(),
729                swaps: vec![swap],
730                native_action: Some(NativeAction::Wrap),
731                ..Default::default()
732            };
733
734            let result = encoder.validate_solution(&solution);
735
736            assert!(result.is_err());
737            assert_eq!(
738                result.err().unwrap(),
739                EncodingError::FatalError(
740                    "Wrapped token must be the first swap's input in order to wrap".to_string()
741                )
742            );
743        }
744
745        #[test]
746        fn test_validate_fails_no_swaps() {
747            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
748            let solution = Solution {
749                exact_out: false,
750                given_token: eth(),
751                swaps: vec![],
752                native_action: Some(NativeAction::Wrap),
753                ..Default::default()
754            };
755
756            let result = encoder.validate_solution(&solution);
757
758            assert!(result.is_err());
759            assert_eq!(
760                result.err().unwrap(),
761                EncodingError::FatalError("No swaps found in solution".to_string())
762            );
763        }
764
765        #[test]
766        fn test_validate_passes_for_unwrap() {
767            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
768            let swap = SwapBuilder::new(
769                ProtocolComponent {
770                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
771                    protocol_system: "uniswap_v2".to_string(),
772                    ..Default::default()
773                },
774                dai().clone(),
775                weth().clone(),
776            )
777            .build();
778
779            let solution = Solution {
780                exact_out: false,
781                checked_token: eth(),
782                swaps: vec![swap],
783                native_action: Some(NativeAction::Unwrap),
784                ..Default::default()
785            };
786
787            let result = encoder.validate_solution(&solution);
788
789            assert!(result.is_ok());
790        }
791
792        #[test]
793        fn test_validate_fails_for_unwrap_wrong_output() {
794            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
795            let swap = SwapBuilder::new(
796                ProtocolComponent {
797                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
798                    protocol_system: "uniswap_v2".to_string(),
799                    ..Default::default()
800                },
801                dai().clone(),
802                weth().clone(),
803            )
804            .build();
805
806            let solution = Solution {
807                exact_out: false,
808                given_token: dai(),
809                checked_token: weth(),
810                swaps: vec![swap],
811                native_action: Some(NativeAction::Unwrap),
812                ..Default::default()
813            };
814
815            let result = encoder.validate_solution(&solution);
816
817            assert!(result.is_err());
818            assert_eq!(
819                result.err().unwrap(),
820                EncodingError::FatalError(
821                    "Native token must be the output token in order to unwrap".to_string()
822                )
823            );
824        }
825
826        #[test]
827        fn test_validate_fails_for_unwrap_wrong_last_swap() {
828            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
829            let swap = SwapBuilder::new(
830                ProtocolComponent {
831                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
832                    protocol_system: "uniswap_v2".to_string(),
833                    ..Default::default()
834                },
835                dai().clone(),
836                eth().clone(),
837            )
838            .build();
839
840            let solution = Solution {
841                exact_out: false,
842                checked_token: eth(),
843                swaps: vec![swap],
844                native_action: Some(NativeAction::Unwrap),
845                ..Default::default()
846            };
847
848            let result = encoder.validate_solution(&solution);
849
850            assert!(result.is_err());
851            assert_eq!(
852                result.err().unwrap(),
853                EncodingError::FatalError(
854                    "Wrapped token must be the last swap's output in order to unwrap".to_string()
855                )
856            );
857        }
858
859        #[test]
860        fn test_validate_cyclical_swap() {
861            // This validation passes because the cyclical swap is the first and last token
862            //      50% ->  WETH
863            // DAI -              -> DAI
864            //      50% -> WETH
865            // (some of the pool addresses in this test are fake)
866            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
867            let swaps = vec![
868                SwapBuilder::new(
869                    ProtocolComponent {
870                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
871                        protocol_system: "uniswap_v2".to_string(),
872                        ..Default::default()
873                    },
874                    dai().clone(),
875                    weth().clone(),
876                )
877                .build(),
878                SwapBuilder::new(
879                    ProtocolComponent {
880                        id: "0x0000000000000000000000000000000000000000".to_string(),
881                        protocol_system: "uniswap_v2".to_string(),
882                        ..Default::default()
883                    },
884                    dai().clone(),
885                    weth().clone(),
886                )
887                .build(),
888                SwapBuilder::new(
889                    ProtocolComponent {
890                        id: "0x0000000000000000000000000000000000000000".to_string(),
891                        protocol_system: "uniswap_v2".to_string(),
892                        ..Default::default()
893                    },
894                    weth().clone(),
895                    dai().clone(),
896                )
897                .build(),
898            ];
899
900            let solution = Solution {
901                exact_out: false,
902                given_token: dai(),
903                checked_token: dai(),
904                swaps,
905                ..Default::default()
906            };
907
908            let result = encoder.validate_solution(&solution);
909
910            assert!(result.is_ok());
911        }
912
913        #[test]
914        fn test_validate_cyclical_swap_fail() {
915            // This test should fail because the cyclical swap is not the first and last token
916            // DAI -> WETH -> USDC -> DAI -> WBTC
917            // (some of the pool addresses in this test are fake)
918            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
919            let swaps = vec![
920                SwapBuilder::new(
921                    ProtocolComponent {
922                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
923                        protocol_system: "uniswap_v2".to_string(),
924                        ..Default::default()
925                    },
926                    dai().clone(),
927                    weth().clone(),
928                )
929                .build(),
930                SwapBuilder::new(
931                    ProtocolComponent {
932                        id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(),
933                        protocol_system: "uniswap_v2".to_string(),
934                        ..Default::default()
935                    },
936                    weth().clone(),
937                    usdc().clone(),
938                )
939                .build(),
940                SwapBuilder::new(
941                    ProtocolComponent {
942                        id: "0x0000000000000000000000000000000000000000".to_string(),
943                        protocol_system: "uniswap_v2".to_string(),
944                        ..Default::default()
945                    },
946                    usdc().clone(),
947                    dai().clone(),
948                )
949                .build(),
950                SwapBuilder::new(
951                    ProtocolComponent {
952                        id: "0x0000000000000000000000000000000000000000".to_string(),
953                        protocol_system: "uniswap_v2".to_string(),
954                        ..Default::default()
955                    },
956                    dai().clone(),
957                    wbtc().clone(),
958                )
959                .build(),
960            ];
961
962            let solution = Solution {
963                exact_out: false,
964                given_token: dai(),
965                checked_token: wbtc(),
966                swaps,
967                ..Default::default()
968            };
969
970            let result = encoder.validate_solution(&solution);
971
972            assert!(result.is_err());
973            assert_eq!(
974            result.err().unwrap(),
975            EncodingError::FatalError(
976                "Cyclical swaps are only allowed if they are the first and last token of a solution".to_string()
977            )
978        );
979        }
980        #[test]
981        fn test_validate_cyclical_swap_split_output() {
982            // This validation passes because it is a valid cyclical swap
983            //             -> WETH
984            // WETH -> DAI
985            //             -> WETH
986            // (some of the pool addresses in this test are fake)
987            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
988            let swaps = vec![
989                SwapBuilder::new(
990                    ProtocolComponent {
991                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
992                        protocol_system: "uniswap_v2".to_string(),
993                        ..Default::default()
994                    },
995                    weth(),
996                    dai(),
997                )
998                .build(),
999                SwapBuilder::new(
1000                    ProtocolComponent {
1001                        id: "0x0000000000000000000000000000000000000000".to_string(),
1002                        protocol_system: "uniswap_v2".to_string(),
1003                        ..Default::default()
1004                    },
1005                    dai(),
1006                    weth(),
1007                )
1008                .split(0.5)
1009                .build(),
1010                SwapBuilder::new(
1011                    ProtocolComponent {
1012                        id: "0x0000000000000000000000000000000000000000".to_string(),
1013                        protocol_system: "uniswap_v2".to_string(),
1014                        ..Default::default()
1015                    },
1016                    dai(),
1017                    weth(),
1018                )
1019                .build(),
1020            ];
1021
1022            let solution = Solution {
1023                exact_out: false,
1024                given_token: weth(),
1025                checked_token: weth(),
1026                swaps,
1027                ..Default::default()
1028            };
1029
1030            let result = encoder.validate_solution(&solution);
1031
1032            assert!(result.is_ok());
1033        }
1034
1035        #[test]
1036        fn test_validate_cyclical_swap_native_action_fail() {
1037            // This validation fails because there is a native action with a valid cyclical swap
1038            // ETH -> WETH -> DAI -> WETH
1039            // (some of the pool addresses in this test are fake)
1040            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
1041            let swaps = vec![
1042                SwapBuilder::new(
1043                    ProtocolComponent {
1044                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
1045                        protocol_system: "uniswap_v2".to_string(),
1046                        ..Default::default()
1047                    },
1048                    weth(),
1049                    dai(),
1050                )
1051                .build(),
1052                SwapBuilder::new(
1053                    ProtocolComponent {
1054                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
1055                        protocol_system: "uniswap_v2".to_string(),
1056                        ..Default::default()
1057                    },
1058                    dai(),
1059                    weth(),
1060                )
1061                .build(),
1062            ];
1063
1064            let solution = Solution {
1065                exact_out: false,
1066                given_token: eth(),
1067                checked_token: weth(),
1068                swaps,
1069                native_action: Some(NativeAction::Wrap),
1070                ..Default::default()
1071            };
1072
1073            let result = encoder.validate_solution(&solution);
1074
1075            assert!(result.is_err());
1076            assert_eq!(
1077                result.err().unwrap(),
1078                EncodingError::FatalError(
1079                    "Wrapping/Unwrapping is not available in cyclical swaps"
1080                        .to_string()
1081                        .to_string()
1082                )
1083            );
1084        }
1085    }
1086
1087    mod executor_encoder {
1088        use std::str::FromStr;
1089
1090        use alloy::hex::encode;
1091        use num_bigint::BigUint;
1092        use tycho_common::{models::protocol::ProtocolComponent, Bytes};
1093
1094        use super::*;
1095        use crate::encoding::models::Solution;
1096
1097        #[test]
1098        fn test_executor_encoder_encode() {
1099            let swap_encoder_registry = get_swap_encoder_registry();
1100            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();
1101
1102            let token_in = weth();
1103            let token_out = dai();
1104
1105            let swap = SwapBuilder::new(
1106                ProtocolComponent {
1107                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
1108                    protocol_system: "uniswap_v2".to_string(),
1109                    ..Default::default()
1110                },
1111                token_in.clone(),
1112                token_out.clone(),
1113            )
1114            .build();
1115
1116            let solution = Solution {
1117                exact_out: false,
1118                given_token: token_in,
1119                given_amount: BigUint::from(1000000000000000000u64),
1120                checked_token: token_out,
1121                checked_amount: BigUint::from(1000000000000000000u64),
1122                sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
1123                // The receiver was generated with `makeAddr("bob") using forge`
1124                receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
1125                swaps: vec![swap],
1126                native_action: None,
1127            };
1128
1129            let encoded_solutions = encoder
1130                .encode_solutions(vec![solution])
1131                .unwrap();
1132            let encoded = encoded_solutions
1133                .first()
1134                .expect("Expected at least one encoded solution");
1135            let hex_protocol_data = encode(&encoded.swaps);
1136            assert_eq!(
1137                encoded.interacting_with,
1138                Bytes::from_str("0x5615deb798bb3e4dfa0139dfa1b3d433cc23b72f").unwrap()
1139            );
1140            assert_eq!(
1141                hex_protocol_data,
1142                String::from(concat!(
1143                    // in token
1144                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
1145                    // component id
1146                    "a478c2975ab1ea89e8196811f51a7b7ade33eb11",
1147                    // receiver
1148                    "1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e",
1149                    // zero for one
1150                    "00",
1151                    // transfer true
1152                    "01",
1153                ))
1154            );
1155        }
1156
1157        #[test]
1158        fn test_executor_encoder_too_many_swaps() {
1159            let swap_encoder_registry = get_swap_encoder_registry();
1160            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();
1161
1162            let token_in = weth();
1163            let token_out = dai();
1164
1165            let swap = SwapBuilder::new(
1166                ProtocolComponent {
1167                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
1168                    protocol_system: "uniswap_v2".to_string(),
1169                    ..Default::default()
1170                },
1171                token_in.clone(),
1172                token_out.clone(),
1173            )
1174            .build();
1175
1176            let solution = Solution {
1177                exact_out: false,
1178                given_token: token_in,
1179                given_amount: BigUint::from(1000000000000000000u64),
1180                checked_token: token_out,
1181                checked_amount: BigUint::from(1000000000000000000u64),
1182                sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
1183                receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
1184                swaps: vec![swap.clone(), swap],
1185                native_action: None,
1186            };
1187
1188            let result = encoder.encode_solutions(vec![solution]);
1189            assert!(result.is_err());
1190        }
1191
1192        #[test]
1193        fn test_executor_encoder_grouped_swaps() {
1194            let swap_encoder_registry = get_swap_encoder_registry();
1195            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();
1196
1197            let usdc = usdc();
1198            let pepe = pepe();
1199
1200            let solution = Solution {
1201                exact_out: false,
1202                given_token: usdc,
1203                given_amount: BigUint::from_str("1000_000000").unwrap(),
1204                checked_token: pepe,
1205                checked_amount: BigUint::from(1000000000000000000u64),
1206                sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
1207                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
1208                swaps: vec![swap_usdc_eth_univ4(), swap_eth_pepe_univ4()],
1209                ..Default::default()
1210            };
1211
1212            let encoded_solutions = encoder
1213                .encode_solutions(vec![solution])
1214                .unwrap();
1215            let encoded_solution = encoded_solutions
1216                .first()
1217                .expect("Expected at least one encoded solution");
1218            let hex_protocol_data = encode(&encoded_solution.swaps);
1219            assert_eq!(
1220                encoded_solution.interacting_with,
1221                Bytes::from_str("0xf62849f9a0b5bf2913b396098f7c7019b51a820a").unwrap()
1222            );
1223            assert_eq!(
1224                hex_protocol_data,
1225                String::from(concat!(
1226                    // group in token
1227                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
1228                    // group out token
1229                    "6982508145454ce325ddbe47a25d4ec3d2311933",
1230                    // zero for one
1231                    "00",
1232                    // transfer type Transfer
1233                    "01",
1234                    // receiver
1235                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2",
1236                    // first pool intermediary token (ETH)
1237                    "0000000000000000000000000000000000000000",
1238                    // fee
1239                    "000bb8",
1240                    // tick spacing
1241                    "00003c",
1242                    // second pool intermediary token (PEPE)
1243                    "6982508145454ce325ddbe47a25d4ec3d2311933",
1244                    // fee
1245                    "0061a8",
1246                    // tick spacing
1247                    "0001f4"
1248                ))
1249            );
1250        }
1251    }
1252}