use std::{collections::HashSet, str::FromStr};

use alloy::signers::local::PrivateKeySigner;
use tycho_common::Bytes;

use crate::encoding::{
    errors::EncodingError,
    evm::{
        approvals::permit2::Permit2,
        constants::{GROUPABLE_PROTOCOLS, IN_TRANSFER_REQUIRED_PROTOCOLS},
        encoding_utils::encode_tycho_router_call,
        group_swaps::group_swaps,
        strategy_encoder::strategy_encoders::{
            SequentialSwapStrategyEncoder, SingleSwapStrategyEncoder, SplitSwapStrategyEncoder,
        },
        swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
    },
    models::{
        Chain, EncodedSolution, EncodingContext, NativeAction, Solution, Transaction, TransferType,
        UserTransferType,
    },
    strategy_encoder::StrategyEncoder,
    tycho_encoder::TychoEncoder,
};

/// Encodes solutions to be used by the TychoRouter.
///
/// # Fields
/// * `chain`: Chain to be used
/// * `single_swap_strategy`: Encoder for single swaps
/// * `sequential_swap_strategy`: Encoder for sequential swaps
/// * `split_swap_strategy`: Encoder for split swaps
/// * `router_address`: Address of the Tycho router contract
/// * `user_transfer_type`: Type of user transfer
/// * `permit2`: Optional Permit2 instance for permit transfers
/// * `signer`: Optional signer (used only for permit2 and full calldata encoding)
#[derive(Clone)]
pub struct TychoRouterEncoder {
    chain: Chain,
    single_swap_strategy: SingleSwapStrategyEncoder,
    sequential_swap_strategy: SequentialSwapStrategyEncoder,
    split_swap_strategy: SplitSwapStrategyEncoder,
    router_address: Bytes,
    user_transfer_type: UserTransferType,
    permit2: Option<Permit2>,
    signer: Option<PrivateKeySigner>,
}

impl TychoRouterEncoder {
    pub fn new(
        chain: Chain,
        swap_encoder_registry: SwapEncoderRegistry,
        router_address: Bytes,
        user_transfer_type: UserTransferType,
        signer: Option<PrivateKeySigner>,
    ) -> Result<Self, EncodingError> {
        let permit2 = if user_transfer_type == UserTransferType::TransferFromPermit2 {
            Some(Permit2::new()?)
        } else {
            None
        };
        Ok(TychoRouterEncoder {
            single_swap_strategy: SingleSwapStrategyEncoder::new(
                chain.clone(),
                swap_encoder_registry.clone(),
                user_transfer_type.clone(),
                router_address.clone(),
            )?,
            sequential_swap_strategy: SequentialSwapStrategyEncoder::new(
                chain.clone(),
                swap_encoder_registry.clone(),
                user_transfer_type.clone(),
                router_address.clone(),
            )?,
            split_swap_strategy: SplitSwapStrategyEncoder::new(
                chain.clone(),
                swap_encoder_registry,
                user_transfer_type.clone(),
                router_address.clone(),
            )?,
            router_address,
            permit2,
            signer,
            chain,
            user_transfer_type,
        })
    }

    fn encode_solution(&self, solution: &Solution) -> Result<EncodedSolution, EncodingError> {
        self.validate_solution(solution)?;
        let protocols: HashSet<String> = solution
            .clone()
            .swaps
            .into_iter()
            .map(|swap| swap.component.protocol_system)
            .collect();

        let mut encoded_solution = if (solution.swaps.len() == 1) ||
            (protocols.len() == 1 &&
                protocols
                    .iter()
                    .any(|p| GROUPABLE_PROTOCOLS.contains(&p.as_str())))
        {
            self.single_swap_strategy
                .encode_strategy(solution.clone())?
        } else if solution
            .swaps
            .iter()
            .all(|swap| swap.split == 0.0)
        {
            self.sequential_swap_strategy
                .encode_strategy(solution.clone())?
        } else {
            self.split_swap_strategy
                .encode_strategy(solution.clone())?
        };

        if let Some(permit2) = self.permit2.clone() {
            let permit = permit2.get_permit(
                &self.router_address,
                &solution.sender,
                &solution.given_token,
                &solution.given_amount,
            )?;
            encoded_solution.permit = Some(permit);
        }
        Ok(encoded_solution)
    }
}

impl TychoEncoder for TychoRouterEncoder {
    fn encode_solutions(
        &self,
        solutions: Vec<Solution>,
    ) -> Result<Vec<EncodedSolution>, EncodingError> {
        let mut result: Vec<EncodedSolution> = Vec::new();
        for solution in solutions.iter() {
            let encoded_solution = self.encode_solution(solution)?;
            result.push(encoded_solution);
        }
        Ok(result)
    }

    fn encode_full_calldata(
        &self,
        solutions: Vec<Solution>,
    ) -> Result<Vec<Transaction>, EncodingError> {
        let mut transactions: Vec<Transaction> = Vec::new();
        for solution in solutions.iter() {
            let encoded_solution = self.encode_solution(solution)?;

            let transaction = encode_tycho_router_call(
                self.chain.id,
                encoded_solution,
                solution,
                self.user_transfer_type.clone(),
                self.chain.native_token()?.clone(),
                self.signer.clone(),
            )?;

            transactions.push(transaction);
        }
        Ok(transactions)
    }

    /// Raises an `EncodingError` if the solution is not considered valid.
    ///
    /// A solution is considered valid if all the following conditions are met:
    /// * The solution is not exact out.
    /// * The solution has at least one swap.
    /// * If the solution is wrapping, the given token is the chain's native token and the first
    ///   swap's input is the chain's wrapped token.
    /// * If the solution is unwrapping, the checked token is the chain's native token and the last
    ///   swap's output is the chain's wrapped token.
    /// * The token cannot appear more than once in the solution unless it is the first and last
    ///   token (i.e. a true cyclical swap).
    fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError> {
        if solution.exact_out {
            return Err(EncodingError::FatalError(
                "Currently only exact input solutions are supported".to_string(),
            ));
        }
        if solution.swaps.is_empty() {
            return Err(EncodingError::FatalError("No swaps found in solution".to_string()));
        }
        let native_address = self.chain.native_token()?;
        let wrapped_address = self.chain.wrapped_token()?;
        if let Some(native_action) = solution.clone().native_action {
            if native_action == NativeAction::Wrap {
                if solution.given_token != native_address {
                    return Err(EncodingError::FatalError(
                        "Native token must be the input token in order to wrap".to_string(),
                    ));
                }
                if let Some(first_swap) = solution.swaps.first() {
                    if first_swap.token_in != wrapped_address {
                        return Err(EncodingError::FatalError(
                            "Wrapped token must be the first swap's input in order to wrap"
                                .to_string(),
                        ));
                    }
                }
            } else if native_action == NativeAction::Unwrap {
                if solution.checked_token != native_address {
                    return Err(EncodingError::FatalError(
                        "Native token must be the output token in order to unwrap".to_string(),
                    ));
                }
                if let Some(last_swap) = solution.swaps.last() {
                    if last_swap.token_out != wrapped_address {
                        return Err(EncodingError::FatalError(
                            "Wrapped token must be the last swap's output in order to unwrap"
                                .to_string(),
                        ));
                    }
                }
            }
        }

        let mut solution_tokens = vec![];
        let mut split_tokens_already_considered = HashSet::new();
        for (i, swap) in solution.swaps.iter().enumerate() {
            // so we don't count the split tokens more than once
            if swap.split != 0.0 {
                if !split_tokens_already_considered.contains(&swap.token_in) {
                    solution_tokens.push(swap.token_in.clone());
                    split_tokens_already_considered.insert(swap.token_in.clone());
                }
            } else {
                // it might be the last swap of the split or a regular swap
                if !split_tokens_already_considered.contains(&swap.token_in) {
                    solution_tokens.push(swap.token_in.clone());
                }
            }
            if i == solution.swaps.len() - 1 {
                solution_tokens.push(swap.token_out.clone());
            }
        }

        if solution_tokens.len() !=
            solution_tokens
                .iter()
                .cloned()
                .collect::<HashSet<Bytes>>()
                .len()
        {
            if let Some(last_swap) = solution.swaps.last() {
                if solution.swaps[0].token_in != last_swap.token_out {
                    return Err(EncodingError::FatalError(
                        "Cyclical swaps are only allowed if they are the first and last token of a solution".to_string(),
                    ));
                } else {
                    // it is a valid cyclical swap
                    // we don't support any wrapping or unwrapping in this case
                    if let Some(_native_action) = solution.clone().native_action {
                        return Err(EncodingError::FatalError(
                            "Wrapping/Unwrapping is not available in cyclical swaps".to_string(),
                        ));
                    }
                }
            }
        }
        Ok(())
    }
}

/// Represents an encoder for one swap to be executed directly against an Executor.
///
/// This is useful when you want to bypass the Tycho Router, use your own Router contract and
/// just need the calldata for a particular swap.
///
/// # Fields
/// * `swap_encoder_registry`: Registry of swap encoders
#[derive(Clone)]
pub struct TychoExecutorEncoder {
    swap_encoder_registry: SwapEncoderRegistry,
}

impl TychoExecutorEncoder {
    pub fn new(swap_encoder_registry: SwapEncoderRegistry) -> Result<Self, EncodingError> {
        Ok(TychoExecutorEncoder { swap_encoder_registry })
    }

    fn encode_executor_calldata(
        &self,
        solution: Solution,
    ) -> Result<EncodedSolution, EncodingError> {
        let grouped_swaps = group_swaps(solution.clone().swaps);
        let number_of_groups = grouped_swaps.len();
        if number_of_groups > 1 {
            return Err(EncodingError::InvalidInput(format!(
                "Tycho executor encoder only supports one swap. Found {number_of_groups}"
            )))
        }

        let grouped_swap = grouped_swaps
            .first()
            .ok_or_else(|| EncodingError::FatalError("Swap grouping failed".to_string()))?;

        let receiver = solution.receiver;

        let swap_encoder = self
            .swap_encoder_registry
            .get_encoder(&grouped_swap.protocol_system)
            .ok_or_else(|| {
                EncodingError::InvalidInput(format!(
                    "Swap encoder not found for protocol: {}",
                    grouped_swap.protocol_system
                ))
            })?;

        let mut grouped_protocol_data: Vec<u8> = vec![];
        for swap in grouped_swap.swaps.iter() {
            let transfer = if IN_TRANSFER_REQUIRED_PROTOCOLS
                .contains(&swap.component.protocol_system.as_str())
            {
                TransferType::Transfer
            } else {
                TransferType::None
            };
            let encoding_context = EncodingContext {
                receiver: receiver.clone(),
                exact_out: solution.exact_out,
                router_address: None,
                group_token_in: grouped_swap.token_in.clone(),
                group_token_out: grouped_swap.token_out.clone(),
                transfer_type: transfer,
            };
            let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
            grouped_protocol_data.extend(protocol_data);
        }

        let executor_address = Bytes::from_str(swap_encoder.executor_address())
            .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;

        Ok(EncodedSolution {
            swaps: grouped_protocol_data,
            interacting_with: executor_address,
            permit: None,
            function_signature: "".to_string(),
            n_tokens: 0,
        })
    }
}

impl TychoEncoder for TychoExecutorEncoder {
    fn encode_solutions(
        &self,
        solutions: Vec<Solution>,
    ) -> Result<Vec<EncodedSolution>, EncodingError> {
        let solution = solutions
            .first()
            .ok_or(EncodingError::FatalError("No solutions found".to_string()))?;
        self.validate_solution(solution)?;

        let encoded_solution = self.encode_executor_calldata(solution.clone())?;

        Ok(vec![encoded_solution])
    }

    fn encode_full_calldata(
        &self,
        _solutions: Vec<Solution>,
    ) -> Result<Vec<Transaction>, EncodingError> {
        Err(EncodingError::NotImplementedError(
            "Full calldata encoding is not supported for TychoExecutorEncoder".to_string(),
        ))
    }

    /// Raises an `EncodingError` if the solution is not considered valid.
    ///
    /// A solution is considered valid if all the following conditions are met:
    /// * The solution is not exact out.
    fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError> {
        if solution.exact_out {
            return Err(EncodingError::FatalError(
                "Currently only exact input solutions are supported".to_string(),
            ));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, str::FromStr};

    use num_bigint::{BigInt, BigUint};
    use tycho_common::models::{protocol::ProtocolComponent, Chain as TychoCommonChain};

    use super::*;
    use crate::encoding::models::Swap;

    fn dai() -> Bytes {
        Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap()
    }

    fn eth() -> Bytes {
        Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap()
    }

    fn weth() -> Bytes {
        Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap()
    }

    fn usdc() -> Bytes {
        Bytes::from_str("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48").unwrap()
    }

    fn wbtc() -> Bytes {
        Bytes::from_str("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599").unwrap()
    }

    fn pepe() -> Bytes {
        Bytes::from_str("0x6982508145454Ce325dDbE47a25d4ec3d2311933").unwrap()
    }

    // Fee and tick spacing information for this test is obtained by querying the
    // USV4 Position Manager contract: 0xbd216513d74c8cf14cf4747e6aaa6420ff64ee9e
    // Using the poolKeys function with the first 25 bytes of the pool id
    fn swap_usdc_eth_univ4() -> Swap {
        let pool_fee_usdc_eth = Bytes::from(BigInt::from(3000).to_signed_bytes_be());
        let tick_spacing_usdc_eth = Bytes::from(BigInt::from(60).to_signed_bytes_be());
        let mut static_attributes_usdc_eth: HashMap<String, Bytes> = HashMap::new();
        static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth);
        static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth);
        Swap {
            component: ProtocolComponent {
                id: "0xdce6394339af00981949f5f3baf27e3610c76326a700af57e4b3e3ae4977f78d"
                    .to_string(),
                protocol_system: "uniswap_v4".to_string(),
                static_attributes: static_attributes_usdc_eth,
                ..Default::default()
            },
            token_in: usdc().clone(),
            token_out: eth().clone(),
            split: 0f64,
        }
    }

    fn swap_eth_pepe_univ4() -> Swap {
        let pool_fee_eth_pepe = Bytes::from(BigInt::from(25000).to_signed_bytes_be());
        let tick_spacing_eth_pepe = Bytes::from(BigInt::from(500).to_signed_bytes_be());
        let mut static_attributes_eth_pepe: HashMap<String, Bytes> = HashMap::new();
        static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe);
        static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe);
        Swap {
            component: ProtocolComponent {
                id: "0xecd73ecbf77219f21f129c8836d5d686bbc27d264742ddad620500e3e548e2c9"
                    .to_string(),
                protocol_system: "uniswap_v4".to_string(),
                static_attributes: static_attributes_eth_pepe,
                ..Default::default()
            },
            token_in: eth().clone(),
            token_out: pepe().clone(),
            split: 0f64,
        }
    }

    fn router_address() -> Bytes {
        Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap()
    }

    fn eth_chain() -> Chain {
        TychoCommonChain::Ethereum.into()
    }

    fn get_swap_encoder_registry() -> SwapEncoderRegistry {
        SwapEncoderRegistry::new(
            Some("config/test_executor_addresses.json".to_string()),
            eth_chain(),
        )
        .unwrap()
    }

    fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> TychoRouterEncoder {
        TychoRouterEncoder::new(
            eth_chain(),
            get_swap_encoder_registry(),
            router_address(),
            user_transfer_type,
            None,
        )
        .unwrap()
    }

    mod router_encoder {
        use super::*;

        #[test]
        #[allow(deprecated)]
        fn test_encode_router_calldata_single_swap() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let eth_amount_in = BigUint::from(1000u32);
            let swap = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: weth(),
                token_out: dai(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                given_amount: eth_amount_in.clone(),
                given_token: eth(),
                checked_token: dai(),
                swaps: vec![swap],
                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                native_action: Some(NativeAction::Wrap),
                ..Default::default()
            };

            let transactions = encoder.encode_full_calldata(vec![solution]);
            assert!(transactions.is_ok());
            let transactions = transactions.unwrap();
            assert_eq!(transactions.len(), 1);
            assert_eq!(transactions[0].value, eth_amount_in);
            assert_eq!(
                transactions[0].to,
                Bytes::from_str("0x3ede3eca2a72b3aecc820e955b36f38437d01395").unwrap()
            );
            // single swap selector
            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "5c4b639c");
        }

        #[test]
        #[allow(deprecated)]
        fn test_encode_router_calldata_single_swap_group() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let solution = Solution {
                exact_out: false,
                given_token: usdc(),
                given_amount: BigUint::from_str("1000_000000").unwrap(),
                checked_token: pepe(),
                checked_amount: BigUint::from_str("105_152_000000000000000000").unwrap(),
                sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                swaps: vec![swap_usdc_eth_univ4(), swap_eth_pepe_univ4()],
                ..Default::default()
            };

            let transactions = encoder.encode_full_calldata(vec![solution]);
            assert!(transactions.is_ok());
            let transactions = transactions.unwrap();
            assert_eq!(transactions.len(), 1);
            // single swap selector
            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "5c4b639c");
        }

        #[test]
        #[allow(deprecated)]
        fn test_encode_router_calldata_sequential_swap() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let eth_amount_in = BigUint::from(1000u32);
            let swap_weth_dai = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: weth(),
                token_out: dai(),
                split: 0f64,
            };

            let swap_dai_usdc = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: dai(),
                token_out: usdc(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                given_amount: eth_amount_in.clone(),
                given_token: eth(),
                checked_token: usdc(),
                swaps: vec![swap_weth_dai, swap_dai_usdc],
                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                native_action: Some(NativeAction::Wrap),
                checked_amount: BigUint::from(1000u32),
                ..Default::default()
            };

            let transactions = encoder.encode_full_calldata(vec![solution]);
            assert!(transactions.is_ok());
            let transactions = transactions.unwrap();
            assert_eq!(transactions.len(), 1);
            assert_eq!(transactions[0].value, eth_amount_in);
            // sequential swap selector
            assert_eq!(&hex::encode(transactions[0].clone().data)[..8], "e21dd0d3");
        }

        #[test]
        fn test_validate_fails_for_exact_out() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let solution = Solution {
                exact_out: true, // This should cause an error
                ..Default::default()
            };
            let result = encoder.validate_solution(&solution);

            assert!(result.is_err());
            assert_eq!(
                result.err().unwrap(),
                EncodingError::FatalError(
                    "Currently only exact input solutions are supported".to_string()
                )
            );
        }

        #[test]
        fn test_validate_passes_for_wrap() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swap = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: weth(),
                token_out: dai(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                given_token: eth(),
                checked_token: dai(),
                swaps: vec![swap],
                native_action: Some(NativeAction::Wrap),
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_ok());
        }

        #[test]
        fn test_validate_fails_for_wrap_wrong_input() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swap = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: weth(),
                token_out: dai(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                given_token: weth(),
                swaps: vec![swap],
                native_action: Some(NativeAction::Wrap),
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_err());
            assert_eq!(
                result.err().unwrap(),
                EncodingError::FatalError(
                    "Native token must be the input token in order to wrap".to_string()
                )
            );
        }

        #[test]
        fn test_validate_fails_for_wrap_wrong_first_swap() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swap = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: eth(),
                token_out: dai(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                given_token: eth(),
                swaps: vec![swap],
                native_action: Some(NativeAction::Wrap),
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_err());
            assert_eq!(
                result.err().unwrap(),
                EncodingError::FatalError(
                    "Wrapped token must be the first swap's input in order to wrap".to_string()
                )
            );
        }

        #[test]
        fn test_validate_fails_no_swaps() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let solution = Solution {
                exact_out: false,
                given_token: eth(),
                swaps: vec![],
                native_action: Some(NativeAction::Wrap),
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_err());
            assert_eq!(
                result.err().unwrap(),
                EncodingError::FatalError("No swaps found in solution".to_string())
            );
        }

        #[test]
        fn test_validate_passes_for_unwrap() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swap = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: dai(),
                token_out: weth(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                checked_token: eth(),
                swaps: vec![swap],
                native_action: Some(NativeAction::Unwrap),
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_ok());
        }

        #[test]
        fn test_validate_fails_for_unwrap_wrong_output() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swap = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: dai(),
                token_out: weth(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                given_token: dai(),
                checked_token: weth(),
                swaps: vec![swap],
                native_action: Some(NativeAction::Unwrap),
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_err());
            assert_eq!(
                result.err().unwrap(),
                EncodingError::FatalError(
                    "Native token must be the output token in order to unwrap".to_string()
                )
            );
        }

        #[test]
        fn test_validate_fails_for_unwrap_wrong_last_swap() {
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swap = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: dai(),
                token_out: eth(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                checked_token: eth(),
                swaps: vec![swap],
                native_action: Some(NativeAction::Unwrap),
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_err());
            assert_eq!(
                result.err().unwrap(),
                EncodingError::FatalError(
                    "Wrapped token must be the last swap's output in order to unwrap".to_string()
                )
            );
        }

        #[test]
        fn test_validate_cyclical_swap() {
            // This validation passes because the cyclical swap is the first and last token
            //      50% ->  WETH
            // DAI -              -> DAI
            //      50% -> WETH
            // (some of the pool addresses in this test are fake)
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swaps = vec![
                Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai(),
                    token_out: weth(),
                    split: 0.5f64,
                },
                Swap {
                    component: ProtocolComponent {
                        id: "0x0000000000000000000000000000000000000000".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai(),
                    token_out: weth(),
                    split: 0f64,
                },
                Swap {
                    component: ProtocolComponent {
                        id: "0x0000000000000000000000000000000000000000".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth(),
                    token_out: dai(),
                    split: 0f64,
                },
            ];

            let solution = Solution {
                exact_out: false,
                given_token: dai(),
                checked_token: dai(),
                swaps,
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_ok());
        }

        #[test]
        fn test_validate_cyclical_swap_fail() {
            // This test should fail because the cyclical swap is not the first and last token
            // DAI -> WETH -> USDC -> DAI -> WBTC
            // (some of the pool addresses in this test are fake)
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swaps = vec![
                Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai(),
                    token_out: weth(),
                    split: 0f64,
                },
                Swap {
                    component: ProtocolComponent {
                        id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth(),
                    token_out: usdc(),
                    split: 0f64,
                },
                Swap {
                    component: ProtocolComponent {
                        id: "0x0000000000000000000000000000000000000000".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: usdc(),
                    token_out: dai(),
                    split: 0f64,
                },
                Swap {
                    component: ProtocolComponent {
                        id: "0x0000000000000000000000000000000000000000".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai(),
                    token_out: wbtc(),
                    split: 0f64,
                },
            ];

            let solution = Solution {
                exact_out: false,
                given_token: dai(),
                checked_token: wbtc(),
                swaps,
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_err());
            assert_eq!(
            result.err().unwrap(),
            EncodingError::FatalError(
                "Cyclical swaps are only allowed if they are the first and last token of a solution".to_string()
            )
        );
        }
        #[test]
        fn test_validate_cyclical_swap_split_output() {
            // This validation passes because it is a valid cyclical swap
            //             -> WETH
            // WETH -> DAI
            //             -> WETH
            // (some of the pool addresses in this test are fake)
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swaps = vec![
                Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth(),
                    token_out: dai(),
                    split: 0f64,
                },
                Swap {
                    component: ProtocolComponent {
                        id: "0x0000000000000000000000000000000000000000".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai(),
                    token_out: weth(),
                    split: 0.5f64,
                },
                Swap {
                    component: ProtocolComponent {
                        id: "0x0000000000000000000000000000000000000000".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai(),
                    token_out: weth(),
                    split: 0f64,
                },
            ];

            let solution = Solution {
                exact_out: false,
                given_token: weth(),
                checked_token: weth(),
                swaps,
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_ok());
        }

        #[test]
        fn test_validate_cyclical_swap_native_action_fail() {
            // This validation fails because there is a native action with a valid cyclical swap
            // ETH -> WETH -> DAI -> WETH
            // (some of the pool addresses in this test are fake)
            let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
            let swaps = vec![
                Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth(),
                    token_out: dai(),
                    split: 0f64,
                },
                Swap {
                    component: ProtocolComponent {
                        id: "0x0000000000000000000000000000000000000000".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai(),
                    token_out: weth(),
                    split: 0f64,
                },
            ];

            let solution = Solution {
                exact_out: false,
                given_token: eth(),
                checked_token: weth(),
                swaps,
                native_action: Some(NativeAction::Wrap),
                ..Default::default()
            };

            let result = encoder.validate_solution(&solution);

            assert!(result.is_err());
            assert_eq!(
                result.err().unwrap(),
                EncodingError::FatalError(
                    "Wrapping/Unwrapping is not available in cyclical swaps"
                        .to_string()
                        .to_string()
                )
            );
        }
    }

    mod executor_encoder {
        use std::str::FromStr;

        use alloy::hex::encode;
        use num_bigint::BigUint;
        use tycho_common::{models::protocol::ProtocolComponent, Bytes};

        use super::*;
        use crate::encoding::models::{Solution, Swap};

        #[test]
        fn test_executor_encoder_encode() {
            let swap_encoder_registry = get_swap_encoder_registry();
            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();

            let token_in = weth();
            let token_out = dai();

            let swap = Swap {
                component: ProtocolComponent {
                    id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: token_in.clone(),
                token_out: token_out.clone(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                given_token: token_in,
                given_amount: BigUint::from(1000000000000000000u64),
                checked_token: token_out,
                checked_amount: BigUint::from(1000000000000000000u64),
                sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
                // The receiver was generated with `makeAddr("bob") using forge`
                receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
                swaps: vec![swap],
                native_action: None,
            };

            let encoded_solutions = encoder
                .encode_solutions(vec![solution])
                .unwrap();
            let encoded = encoded_solutions
                .first()
                .expect("Expected at least one encoded solution");
            let hex_protocol_data = encode(&encoded.swaps);
            assert_eq!(
                encoded.interacting_with,
                Bytes::from_str("0x5615deb798bb3e4dfa0139dfa1b3d433cc23b72f").unwrap()
            );
            assert_eq!(
                hex_protocol_data,
                String::from(concat!(
                    // in token
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                    // component id
                    "a478c2975ab1ea89e8196811f51a7b7ade33eb11",
                    // receiver
                    "1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e",
                    // zero for one
                    "00",
                    // transfer true
                    "01",
                ))
            );
        }

        #[test]
        fn test_executor_encoder_too_many_swaps() {
            let swap_encoder_registry = get_swap_encoder_registry();
            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();

            let token_in = weth();
            let token_out = dai();

            let swap = Swap {
                component: ProtocolComponent {
                    protocol_system: "uniswap_v2".to_string(),
                    ..Default::default()
                },
                token_in: token_in.clone(),
                token_out: token_out.clone(),
                split: 0f64,
            };

            let solution = Solution {
                exact_out: false,
                given_token: token_in,
                given_amount: BigUint::from(1000000000000000000u64),
                checked_token: token_out,
                checked_amount: BigUint::from(1000000000000000000u64),
                sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
                receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
                swaps: vec![swap.clone(), swap],
                native_action: None,
            };

            let result = encoder.encode_solutions(vec![solution]);
            assert!(result.is_err());
        }

        #[test]
        fn test_executor_encoder_grouped_swaps() {
            let swap_encoder_registry = get_swap_encoder_registry();
            let encoder = TychoExecutorEncoder::new(swap_encoder_registry).unwrap();

            let usdc = usdc();
            let pepe = pepe();

            let solution = Solution {
                exact_out: false,
                given_token: usdc,
                given_amount: BigUint::from_str("1000_000000").unwrap(),
                checked_token: pepe,
                checked_amount: BigUint::from(1000000000000000000u64),
                sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                swaps: vec![swap_usdc_eth_univ4(), swap_eth_pepe_univ4()],
                ..Default::default()
            };

            let encoded_solutions = encoder
                .encode_solutions(vec![solution])
                .unwrap();
            let encoded_solution = encoded_solutions
                .first()
                .expect("Expected at least one encoded solution");
            let hex_protocol_data = encode(&encoded_solution.swaps);
            assert_eq!(
                encoded_solution.interacting_with,
                Bytes::from_str("0xf62849f9a0b5bf2913b396098f7c7019b51a820a").unwrap()
            );
            assert_eq!(
                hex_protocol_data,
                String::from(concat!(
                    // group in token
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
                    // group out token
                    "6982508145454ce325ddbe47a25d4ec3d2311933",
                    // zero for one
                    "00",
                    // transfer type Transfer
                    "01",
                    // receiver
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2",
                    // first pool intermediary token (ETH)
                    "0000000000000000000000000000000000000000",
                    // fee
                    "000bb8",
                    // tick spacing
                    "00003c",
                    // second pool intermediary token (PEPE)
                    "6982508145454ce325ddbe47a25d4ec3d2311933",
                    // fee
                    "0061a8",
                    // tick spacing
                    "0001f4"
                ))
            );
        }
    }

    mod integration {
        use std::{collections::HashMap, str::FromStr};

        use alloy::{hex::encode, signers::local::PrivateKeySigner};
        use alloy_primitives::{hex, Address, B256, U256};
        use alloy_sol_types::SolValue;
        use num_bigint::{BigInt, BigUint};
        use tycho_common::{models::protocol::ProtocolComponent, Bytes};

        use super::*;
        use crate::encoding::evm::utils::{biguint_to_u256, write_calldata_to_file};

        fn get_signer() -> PrivateKeySigner {
            // Set up a mock private key for signing (Alice's pk in our contract tests)
            let private_key =
                "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string();

            let pk = B256::from_str(&private_key).unwrap();
            PrivateKeySigner::from_bytes(&pk).unwrap()
        }

        mod single {
            use super::*;

            #[test]
            fn test_single_swap_strategy_encoder() {
                // Performs a single swap from WETH to DAI on a USV2 pool, with no grouping
                // optimizations.
                let checked_amount = BigUint::from_str("2018817438608734439720").unwrap();
                let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
                let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();

                let swap = Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: dai.clone(),
                    split: 0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: weth,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: dai,
                    checked_amount: checked_amount.clone(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    ..Default::default()
                };

                let encoded_solutions = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solutions[0].clone(),
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth(),
                    Some(get_signer()),
                )
                .unwrap()
                .data;
                let expected_min_amount_encoded =
                    encode(U256::abi_encode(&biguint_to_u256(&checked_amount)));
                let expected_input = [
                    "30ace1b1",                                                              // Function selector
                    "0000000000000000000000000000000000000000000000000de0b6b3a7640000",      // amount in
                    "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",      // token in
                    "0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f",      // token out
                    &expected_min_amount_encoded,                                            // min amount out
                    "0000000000000000000000000000000000000000000000000000000000000000",      // wrap
                    "0000000000000000000000000000000000000000000000000000000000000000",      // unwrap
                    "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2",      // receiver
                ]
                    .join("");

                // after this there is the permit and because of the deadlines (that depend on block
                // time) it's hard to assert

                let expected_swap = String::from(concat!(
                    // length of encoded swap without padding
                    "0000000000000000000000000000000000000000000000000000000000000052",
                    // Swap data
                    "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    "00",                                       // zero2one
                    "00",                                       // transfer type TransferFrom
                    "0000000000000000000000000000",             // padding
                ));
                let hex_calldata = encode(&calldata);

                assert_eq!(hex_calldata[..456], expected_input);
                assert_eq!(hex_calldata[1224..], expected_swap);
                write_calldata_to_file(
                    "test_single_swap_strategy_encoder",
                    &hex_calldata.to_string(),
                );
            }

            #[test]
            fn test_single_swap_strategy_encoder_no_permit2() {
                // Performs a single swap from WETH to DAI on a USV2 pool, without permit2 and no
                // grouping optimizations.

                let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
                let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();

                let checked_amount = BigUint::from_str("1_640_000000000000000000").unwrap();
                let expected_min_amount = U256::from_str("1_640_000000000000000000").unwrap();

                let swap = Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: dai.clone(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                let solution = Solution {
                    exact_out: false,
                    given_token: weth,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: dai,
                    checked_amount,
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();
                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFrom,
                    eth(),
                    None,
                )
                .unwrap()
                .data;
                let expected_min_amount_encoded = encode(U256::abi_encode(&expected_min_amount));
                let expected_input = [
                    "5c4b639c",                                                           // Function selector
                    "0000000000000000000000000000000000000000000000000de0b6b3a7640000",   // amount in
                    "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",   // token in
                    "0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f",   // token out
                    &expected_min_amount_encoded,                                         // min amount out
                    "0000000000000000000000000000000000000000000000000000000000000000",   // wrap
                    "0000000000000000000000000000000000000000000000000000000000000000",   // unwrap
                    "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2",   // receiver
                    "0000000000000000000000000000000000000000000000000000000000000001",   // transfer from needed
                    "0000000000000000000000000000000000000000000000000000000000000120",   // offset of swap bytes
                    "0000000000000000000000000000000000000000000000000000000000000052",   // length of swap bytes without padding

                    // Swap data
                    "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    "00",                                       // zero2one
                    "00",                                       // transfer type TransferFrom
                    "0000000000000000000000000000",             // padding
                ]
                    .join("");

                let hex_calldata = encode(&calldata);

                assert_eq!(hex_calldata, expected_input);
                write_calldata_to_file(
                    "test_single_swap_strategy_encoder_no_permit2",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_swap_strategy_encoder_no_transfer_in() {
                // Performs a single swap from WETH to DAI on a USV2 pool assuming that the tokens
                // are already in the router

                let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
                let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();

                let checked_amount = BigUint::from_str("1_640_000000000000000000").unwrap();
                let expected_min_amount = U256::from_str("1_640_000000000000000000").unwrap();

                let swap = Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: dai.clone(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::None);

                let solution = Solution {
                    exact_out: false,
                    given_token: weth,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: dai,
                    checked_amount,
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();
                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::None,
                    eth(),
                    None,
                )
                .unwrap()
                .data;
                let expected_min_amount_encoded = encode(U256::abi_encode(&expected_min_amount));
                let expected_input = [
                    "5c4b639c",                                                           // Function selector
                    "0000000000000000000000000000000000000000000000000de0b6b3a7640000",   // amount in
                    "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",   // token in
                    "0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f",   // token out
                    &expected_min_amount_encoded,                                         // min amount out
                    "0000000000000000000000000000000000000000000000000000000000000000",   // wrap
                    "0000000000000000000000000000000000000000000000000000000000000000",   // unwrap
                    "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2",   // receiver
                    "0000000000000000000000000000000000000000000000000000000000000000",   // transfer from not needed
                    "0000000000000000000000000000000000000000000000000000000000000120",   // offset of swap bytes
                    "0000000000000000000000000000000000000000000000000000000000000052",   // length of swap bytes without padding

                    // Swap data
                    "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    "00",                                       // zero2one
                    "01",                                       // transfer type Transfer
                    "0000000000000000000000000000",             // padding
                ]
                    .join("");

                let hex_calldata = encode(&calldata);

                assert_eq!(hex_calldata, expected_input);
                write_calldata_to_file(
                    "test_single_swap_strategy_encoder_no_transfer_in",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_swap_strategy_encoder_wrap() {
                // Performs a single swap from WETH to DAI on a USV2 pool, wrapping ETH
                // Note: This test does not assert anything. It is only used to obtain integration
                // test data for our router solidity test.

                let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();

                let swap = Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth(),
                    token_out: dai.clone(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: eth(),
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: dai,
                    checked_amount: BigUint::from_str("1659881924818443699787").unwrap(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    native_action: Some(NativeAction::Wrap),
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth(),
                    Some(get_signer()),
                )
                .unwrap()
                .data;
                let hex_calldata = encode(&calldata);
                write_calldata_to_file(
                    "test_single_swap_strategy_encoder_wrap",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_swap_strategy_encoder_unwrap() {
                // Performs a single swap from DAI to WETH on a USV2 pool, unwrapping ETH at the end
                // Note: This test does not assert anything. It is only used to obtain integration
                // test data for our router solidity test.

                let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();

                let swap = Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai.clone(),
                    token_out: weth(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: dai,
                    given_amount: BigUint::from_str("3_000_000000000000000000").unwrap(),
                    checked_token: eth(),
                    checked_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    native_action: Some(NativeAction::Unwrap),
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth(),
                    Some(get_signer()),
                )
                .unwrap()
                .data;

                let hex_calldata = encode(&calldata);
                write_calldata_to_file(
                    "test_single_swap_strategy_encoder_unwrap",
                    hex_calldata.as_str(),
                );
            }
        }

        mod sequential {
            use super::*;

            #[test]
            fn test_sequential_swap_strategy_encoder() {
                // Note: This test does not assert anything. It is only used to obtain integration
                // test data for our router solidity test.
                //
                // Performs a sequential swap from WETH to USDC though WBTC using USV2 pools
                //
                //   WETH ───(USV2)──> WBTC ───(USV2)──> USDC

                let weth = weth();
                let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
                let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                let swap_weth_wbtc = Swap {
                    component: ProtocolComponent {
                        id: "0xBb2b8038a1640196FbE3e38816F3e67Cba72D940".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: wbtc.clone(),
                    split: 0f64,
                };
                let swap_wbtc_usdc = Swap {
                    component: ProtocolComponent {
                        id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: wbtc.clone(),
                    token_out: usdc.clone(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: weth,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: usdc,
                    checked_amount: BigUint::from_str("26173932").unwrap(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap_weth_wbtc, swap_wbtc_usdc],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth(),
                    Some(get_signer()),
                )
                .unwrap()
                .data;

                let hex_calldata = encode(&calldata);
                write_calldata_to_file(
                    "test_sequential_swap_strategy_encoder",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_sequential_swap_strategy_encoder_no_permit2() {
                // Performs a sequential swap from WETH to USDC though WBTC using USV2 pools
                //
                //   WETH ───(USV2)──> WBTC ───(USV2)──> USDC

                let weth = weth();
                let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
                let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                let swap_weth_wbtc = Swap {
                    component: ProtocolComponent {
                        id: "0xBb2b8038a1640196FbE3e38816F3e67Cba72D940".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: wbtc.clone(),
                    split: 0f64,
                };
                let swap_wbtc_usdc = Swap {
                    component: ProtocolComponent {
                        id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: wbtc.clone(),
                    token_out: usdc.clone(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                let solution = Solution {
                    exact_out: false,
                    given_token: weth,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: usdc,
                    checked_amount: BigUint::from_str("26173932").unwrap(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap_weth_wbtc, swap_wbtc_usdc],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFrom,
                    eth(),
                    None,
                )
                .unwrap()
                .data;

                let hex_calldata = encode(&calldata);

                let expected = String::from(concat!(
                    "e21dd0d3",                                                         /* function selector */
                    "0000000000000000000000000000000000000000000000000de0b6b3a7640000", /* amount in */
                    "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token ou
                    "00000000000000000000000000000000000000000000000000000000018f61ec", /* min amount out */
                    "0000000000000000000000000000000000000000000000000000000000000000", // wrap
                    "0000000000000000000000000000000000000000000000000000000000000000", // unwrap
                    "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    "0000000000000000000000000000000000000000000000000000000000000001", /* transfer from needed */
                    "0000000000000000000000000000000000000000000000000000000000000120", /* length ple
                                                                                         * encode */
                    "00000000000000000000000000000000000000000000000000000000000000a8",
                    // swap 1
                    "0052",                                     // swap length
                    "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "bb2b8038a1640196fbe3e38816f3e67cba72d940", // component id
                    "004375dff511095cc5a197a54140a24efef3a416", // receiver (next pool)
                    "00",                                       // zero to one
                    "00",                                       // transfer type TransferFrom
                    // swap 2
                    "0052",                                             // swap length
                    "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f",         // executor address
                    "2260fac5e5542a773aa44fbcfedf7c193bc2c599",         // token in
                    "004375dff511095cc5a197a54140a24efef3a416",         // component id
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2",         // receiver (final user)
                    "01",                                               // zero to one
                    "02",                                               // transfer type None
                    "000000000000000000000000000000000000000000000000", // padding
                ));

                assert_eq!(hex_calldata, expected);
                write_calldata_to_file(
                    "test_sequential_swap_strategy_encoder_no_permit2",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_sequential_strategy_cyclic_swap() {
                // This test has start and end tokens that are the same
                // The flow is:
                // USDC -> WETH -> USDC  using two pools

                let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
                let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                // Create two Uniswap V3 pools for the cyclic swap
                // USDC -> WETH (Pool 1)
                let swap_usdc_weth = Swap {
                    component: ProtocolComponent {
                        id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(), /* USDC-WETH USV3
                                                                                       * Pool 1 */
                        protocol_system: "uniswap_v3".to_string(),
                        static_attributes: {
                            let mut attrs = HashMap::new();
                            attrs.insert(
                                "fee".to_string(),
                                Bytes::from(BigInt::from(500).to_signed_bytes_be()),
                            );
                            attrs
                        },
                        ..Default::default()
                    },
                    token_in: usdc.clone(),
                    token_out: weth.clone(),
                    split: 0f64,
                };

                // WETH -> USDC (Pool 2)
                let swap_weth_usdc = Swap {
                    component: ProtocolComponent {
                        id: "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8".to_string(), /* USDC-WETH USV3
                                                                                       * Pool 2 */
                        protocol_system: "uniswap_v3".to_string(),
                        static_attributes: {
                            let mut attrs = HashMap::new();
                            attrs.insert(
                                "fee".to_string(),
                                Bytes::from(BigInt::from(3000).to_signed_bytes_be()),
                            );
                            attrs
                        },
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: usdc.clone(),
                    split: 0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: usdc.clone(),
                    given_amount: BigUint::from_str("100000000").unwrap(), // 100 USDC (6 decimals)
                    checked_token: usdc.clone(),
                    checked_amount: BigUint::from_str("99389294").unwrap(), /* Expected output
                                                                             * from test */
                    swaps: vec![swap_usdc_weth, swap_weth_usdc],
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth(),
                    Some(get_signer()),
                )
                .unwrap()
                .data;
                let hex_calldata = hex::encode(&calldata);
                let expected_input = [
                    "51bcc7b6",                                                         // selector
                    "0000000000000000000000000000000000000000000000000000000005f5e100", // given amount
                    "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // given token
                    "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // checked token
                    "0000000000000000000000000000000000000000000000000000000005ec8f6e", // min amount out
                    "0000000000000000000000000000000000000000000000000000000000000000", // wrap action
                    "0000000000000000000000000000000000000000000000000000000000000000", // unwrap action
                    "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                ]
                    .join("");

                let expected_swaps = [
                    "00000000000000000000000000000000000000000000000000000000000000d6",  // length of ple encoded swaps without padding
                    "0069",  // ple encoded swaps
                    "2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
                    "0001f4",                                   // pool fee
                    "3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
                    "88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
                    "01",                                       // zero2one
                    "00",                                       // transfer type TransferFrom
                    "0069",                                     // ple encoded swaps
                    "2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
                    "000bb8",                                   // pool fee
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    "8ad599c3a0ff1de082011efddc58f1908eb6e6d8", // component id
                    "00",                                       // zero2one
                    "01",                                       // transfer type Transfer
                    "00000000000000000000",                     // padding
                ]
                    .join("");

                assert_eq!(hex_calldata[..456], expected_input);
                assert_eq!(hex_calldata[1224..], expected_swaps);
                write_calldata_to_file(
                    "test_sequential_strategy_cyclic_swap",
                    hex_calldata.as_str(),
                );
            }

            mod optimized_transfers {
                // In this module we test the ability to chain swaps or not. Different protocols are
                // tested. The encoded data is used for solidity tests as well
                use super::*;

                #[test]
                fn test_uniswap_v3_uniswap_v2() {
                    // Note: This test does not assert anything. It is only used to obtain
                    // integration test data for our router solidity test.
                    //
                    // Performs a sequential swap from WETH to USDC though WBTC using USV3 and USV2
                    // pools
                    //
                    //   WETH ───(USV3)──> WBTC ───(USV2)──> USDC

                    let weth = weth();
                    let wbtc =
                        Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
                    let usdc =
                        Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                    let swap_weth_wbtc = Swap {
                        component: ProtocolComponent {
                            id: "0xCBCdF9626bC03E24f779434178A73a0B4bad62eD".to_string(),
                            protocol_system: "uniswap_v3".to_string(),
                            static_attributes: {
                                let mut attrs = HashMap::new();
                                attrs.insert(
                                    "fee".to_string(),
                                    Bytes::from(BigInt::from(3000).to_signed_bytes_be()),
                                );
                                attrs
                            },
                            ..Default::default()
                        },
                        token_in: weth.clone(),
                        token_out: wbtc.clone(),
                        split: 0f64,
                    };
                    let swap_wbtc_usdc = Swap {
                        component: ProtocolComponent {
                            id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(),
                            protocol_system: "uniswap_v2".to_string(),
                            ..Default::default()
                        },
                        token_in: wbtc.clone(),
                        token_out: usdc.clone(),
                        split: 0f64,
                    };
                    let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                    let solution = Solution {
                        exact_out: false,
                        given_token: weth,
                        given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                        checked_token: usdc,
                        checked_amount: BigUint::from_str("26173932").unwrap(),
                        sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        swaps: vec![swap_weth_wbtc, swap_wbtc_usdc],
                        ..Default::default()
                    };

                    let encoded_solution = encoder
                        .encode_solutions(vec![solution.clone()])
                        .unwrap()[0]
                        .clone();

                    let calldata = encode_tycho_router_call(
                        eth_chain().id,
                        encoded_solution,
                        &solution,
                        UserTransferType::TransferFrom,
                        eth(),
                        None,
                    )
                    .unwrap()
                    .data;

                    let hex_calldata = encode(&calldata);
                    write_calldata_to_file("test_uniswap_v3_uniswap_v2", hex_calldata.as_str());
                }

                #[test]
                fn test_uniswap_v3_uniswap_v3() {
                    // Note: This test does not assert anything. It is only used to obtain
                    // integration test data for our router solidity test.
                    //
                    // Performs a sequential swap from WETH to USDC though WBTC using USV3 pools
                    // There is no optimization between the two USV3 pools, this is currently not
                    // supported.
                    //
                    //   WETH ───(USV3)──> WBTC ───(USV3)──> USDC

                    let weth = weth();
                    let wbtc =
                        Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
                    let usdc =
                        Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                    let swap_weth_wbtc = Swap {
                        component: ProtocolComponent {
                            id: "0xCBCdF9626bC03E24f779434178A73a0B4bad62eD".to_string(),
                            protocol_system: "uniswap_v3".to_string(),
                            static_attributes: {
                                let mut attrs = HashMap::new();
                                attrs.insert(
                                    "fee".to_string(),
                                    Bytes::from(BigInt::from(3000).to_signed_bytes_be()),
                                );
                                attrs
                            },
                            ..Default::default()
                        },
                        token_in: weth.clone(),
                        token_out: wbtc.clone(),
                        split: 0f64,
                    };
                    let swap_wbtc_usdc = Swap {
                        component: ProtocolComponent {
                            id: "0x99ac8cA7087fA4A2A1FB6357269965A2014ABc35".to_string(),
                            protocol_system: "uniswap_v3".to_string(),
                            static_attributes: {
                                let mut attrs = HashMap::new();
                                attrs.insert(
                                    "fee".to_string(),
                                    Bytes::from(BigInt::from(3000).to_signed_bytes_be()),
                                );
                                attrs
                            },
                            ..Default::default()
                        },
                        token_in: wbtc.clone(),
                        token_out: usdc.clone(),
                        split: 0f64,
                    };
                    let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                    let solution = Solution {
                        exact_out: false,
                        given_token: weth,
                        given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                        checked_token: usdc,
                        checked_amount: BigUint::from_str("26173932").unwrap(),
                        sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        swaps: vec![swap_weth_wbtc, swap_wbtc_usdc],
                        ..Default::default()
                    };

                    let encoded_solution = encoder
                        .encode_solutions(vec![solution.clone()])
                        .unwrap()[0]
                        .clone();

                    let calldata = encode_tycho_router_call(
                        eth_chain().id,
                        encoded_solution,
                        &solution,
                        UserTransferType::TransferFrom,
                        eth(),
                        None,
                    )
                    .unwrap()
                    .data;

                    let hex_calldata = encode(&calldata);
                    write_calldata_to_file("test_uniswap_v3_uniswap_v3", hex_calldata.as_str());
                }

                #[test]
                fn test_uniswap_v3_curve() {
                    // Note: This test does not assert anything. It is only used to obtain
                    // integration test data for our router solidity test.
                    //
                    // Performs a sequential swap from WETH to USDT though WBTC using USV3 and curve
                    // pools
                    //
                    //   WETH ───(USV3)──> WBTC ───(curve)──> USDT

                    let weth = weth();
                    let wbtc =
                        Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
                    let usdt =
                        Bytes::from_str("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap();

                    let swap_weth_wbtc = Swap {
                        component: ProtocolComponent {
                            id: "0xCBCdF9626bC03E24f779434178A73a0B4bad62eD".to_string(),
                            protocol_system: "uniswap_v3".to_string(),
                            static_attributes: {
                                let mut attrs = HashMap::new();
                                attrs.insert(
                                    "fee".to_string(),
                                    Bytes::from(BigInt::from(3000).to_signed_bytes_be()),
                                );
                                attrs
                            },
                            ..Default::default()
                        },
                        token_in: weth.clone(),
                        token_out: wbtc.clone(),
                        split: 0f64,
                    };

                    let swap_wbtc_usdt = Swap {
                        component: ProtocolComponent {
                            id: String::from("0xD51a44d3FaE010294C616388b506AcdA1bfAAE46"),
                            protocol_system: String::from("vm:curve"),
                            static_attributes: {
                                let mut attrs: HashMap<String, Bytes> = HashMap::new();
                                attrs.insert(
                                    "factory".into(),
                                    Bytes::from(
                                        "0x0000000000000000000000000000000000000000"
                                            .as_bytes()
                                            .to_vec(),
                                    ),
                                );
                                attrs.insert(
                                    "coins".into(),
                                    Bytes::from_str("0x5b22307864616331376639353864326565353233613232303632303639393435393763313364383331656337222c22307832323630666163356535353432613737336161343466626366656466376331393362633263353939222c22307863303261616133396232323366653864306130653563346632376561643930383363373536636332225d")
                                        .unwrap(),
                                );
                                attrs
                            },
                            ..Default::default()
                        },
                        token_in: wbtc.clone(),
                        token_out: usdt.clone(),
                        split: 0f64,
                    };
                    let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                    let solution = Solution {
                        exact_out: false,
                        given_token: weth,
                        given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                        checked_token: usdt,
                        checked_amount: BigUint::from_str("26173932").unwrap(),
                        sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        swaps: vec![swap_weth_wbtc, swap_wbtc_usdt],
                        ..Default::default()
                    };

                    let encoded_solution = encoder
                        .encode_solutions(vec![solution.clone()])
                        .unwrap()[0]
                        .clone();

                    let calldata = encode_tycho_router_call(
                        eth_chain().id,
                        encoded_solution,
                        &solution,
                        UserTransferType::TransferFrom,
                        eth(),
                        None,
                    )
                    .unwrap()
                    .data;

                    let hex_calldata = encode(&calldata);
                    write_calldata_to_file("test_uniswap_v3_curve", hex_calldata.as_str());
                }

                #[test]
                fn test_balancer_v2_uniswap_v2() {
                    // Note: This test does not assert anything. It is only used to obtain
                    // integration test data for our router solidity test.
                    //
                    // Performs a sequential swap from WETH to USDC though WBTC using balancer and
                    // USV2 pools
                    //
                    //   WETH ───(balancer)──> WBTC ───(USV2)──> USDC

                    let weth = weth();
                    let wbtc =
                        Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
                    let usdc =
                        Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                    let swap_weth_wbtc = Swap {
                        component: ProtocolComponent {
                            id:
                                "0xa6f548df93de924d73be7d25dc02554c6bd66db500020000000000000000000e"
                                    .to_string(),
                            protocol_system: "vm:balancer_v2".to_string(),
                            ..Default::default()
                        },
                        token_in: weth.clone(),
                        token_out: wbtc.clone(),
                        split: 0f64,
                    };

                    let swap_wbtc_usdc = Swap {
                        component: ProtocolComponent {
                            id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(),
                            protocol_system: "uniswap_v2".to_string(),
                            ..Default::default()
                        },
                        token_in: wbtc.clone(),
                        token_out: usdc.clone(),
                        split: 0f64,
                    };
                    let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                    let solution = Solution {
                        exact_out: false,
                        given_token: weth,
                        given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                        checked_token: usdc,
                        checked_amount: BigUint::from_str("26173932").unwrap(),
                        sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        swaps: vec![swap_weth_wbtc, swap_wbtc_usdc],
                        ..Default::default()
                    };

                    let encoded_solution = encoder
                        .encode_solutions(vec![solution.clone()])
                        .unwrap()[0]
                        .clone();

                    let calldata = encode_tycho_router_call(
                        eth_chain().id,
                        encoded_solution,
                        &solution,
                        UserTransferType::TransferFrom,
                        eth(),
                        None,
                    )
                    .unwrap()
                    .data;

                    let hex_calldata = encode(&calldata);
                    write_calldata_to_file("test_balancer_v2_uniswap_v2", hex_calldata.as_str());
                }

                #[test]
                fn test_multi_protocol() {
                    // Note: This test does not assert anything. It is only used to obtain
                    // integration test data for our router solidity test.
                    //
                    // Performs the following swap:
                    //
                    //   DAI ─(USV2)-> WETH ─(bal)─> WBTC ─(curve)─> USDT ─(ekubo)─> USDC ─(USV4)─>
                    // ETH

                    let weth = weth();
                    let eth = eth();
                    let wbtc =
                        Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
                    let usdc =
                        Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
                    let usdt =
                        Bytes::from_str("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap();
                    let dai =
                        Bytes::from_str("0x6B175474E89094C44Da98b954EedeAC495271d0F").unwrap();

                    let usv2_swap_dai_weth = Swap {
                        component: ProtocolComponent {
                            id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                            protocol_system: "uniswap_v2".to_string(),
                            ..Default::default()
                        },
                        token_in: dai.clone(),
                        token_out: weth.clone(),
                        split: 0f64,
                    };

                    let balancer_swap_weth_wbtc = Swap {
                        component: ProtocolComponent {
                            id:
                                "0xa6f548df93de924d73be7d25dc02554c6bd66db500020000000000000000000e"
                                    .to_string(),
                            protocol_system: "vm:balancer_v2".to_string(),
                            ..Default::default()
                        },
                        token_in: weth.clone(),
                        token_out: wbtc.clone(),
                        split: 0f64,
                    };

                    let curve_swap_wbtc_usdt = Swap {
                        component: ProtocolComponent {
                            id: String::from("0xD51a44d3FaE010294C616388b506AcdA1bfAAE46"),
                            protocol_system: String::from("vm:curve"),
                            static_attributes: {
                                let mut attrs: HashMap<String, Bytes> = HashMap::new();
                                attrs.insert(
                                    "factory".into(),
                                    Bytes::from(
                                        "0x0000000000000000000000000000000000000000"
                                            .as_bytes()
                                            .to_vec(),
                                    ),
                                );
                                attrs.insert(
                                    "coins".into(),
                                    Bytes::from_str("0x5b22307864616331376639353864326565353233613232303632303639393435393763313364383331656337222c22307832323630666163356535353432613737336161343466626366656466376331393362633263353939222c22307863303261616133396232323366653864306130653563346632376561643930383363373536636332225d")
                                        .unwrap(),
                                );
                                attrs
                            },
                            ..Default::default()
                        },
                        token_in: wbtc.clone(),
                        token_out: usdt.clone(),
                        split: 0f64,
                    };

                    // Ekubo

                    let component = ProtocolComponent {
                        // All Ekubo swaps go through the core contract - not necessary to specify
                        // pool id for test
                        protocol_system: "ekubo_v2".to_string(),
                        // 0.0025% fee & 0.005% base pool
                        static_attributes: HashMap::from([
                            ("fee".to_string(), Bytes::from(461168601842738_u64)),
                            ("tick_spacing".to_string(), Bytes::from(50_u32)),
                            ("extension".to_string(), Bytes::zero(20)),
                        ]),
                        ..Default::default()
                    };
                    let ekubo_swap_usdt_usdc = Swap {
                        component,
                        token_in: usdt.clone(),
                        token_out: usdc.clone(),
                        split: 0f64,
                    };

                    // USV4
                    // Fee and tick spacing information for this test is obtained by querying the
                    // USV4 Position Manager contract: 0xbd216513d74c8cf14cf4747e6aaa6420ff64ee9e
                    // Using the poolKeys function with the first 25 bytes of the pool id
                    let pool_fee_usdc_eth = Bytes::from(BigInt::from(3000).to_signed_bytes_be());
                    let tick_spacing_usdc_eth = Bytes::from(BigInt::from(60).to_signed_bytes_be());
                    let mut static_attributes_usdc_eth: HashMap<String, Bytes> = HashMap::new();
                    static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth);
                    static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth);

                    let usv4_swap_usdc_eth = Swap {
                        component: ProtocolComponent {
                            id:
                                "0xdce6394339af00981949f5f3baf27e3610c76326a700af57e4b3e3ae4977f78d"
                                    .to_string(),
                            protocol_system: "uniswap_v4".to_string(),
                            static_attributes: static_attributes_usdc_eth,
                            ..Default::default()
                        },
                        token_in: usdc.clone(),
                        token_out: eth.clone(),
                        split: 0f64,
                    };

                    let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                    // Put all components together
                    let solution = Solution {
                        exact_out: false,
                        given_token: dai,
                        given_amount: BigUint::from_str("1500_000000000000000000").unwrap(),
                        checked_token: eth.clone(),
                        checked_amount: BigUint::from_str("732214216964381330").unwrap(),
                        sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                            .unwrap(),
                        swaps: vec![
                            usv2_swap_dai_weth,
                            balancer_swap_weth_wbtc,
                            curve_swap_wbtc_usdt,
                            ekubo_swap_usdt_usdc,
                            usv4_swap_usdc_eth,
                        ],
                        ..Default::default()
                    };

                    let encoded_solution = encoder
                        .encode_solutions(vec![solution.clone()])
                        .unwrap()[0]
                        .clone();

                    let calldata = encode_tycho_router_call(
                        eth_chain().id,
                        encoded_solution,
                        &solution,
                        UserTransferType::TransferFromPermit2,
                        eth,
                        Some(get_signer()),
                    )
                    .unwrap()
                    .data;

                    let hex_calldata = encode(&calldata);
                    write_calldata_to_file("test_multi_protocol", hex_calldata.as_str());
                }
            }
        }

        mod split {
            use super::*;

            #[test]
            fn test_split_swap_strategy_encoder() {
                // Note: This test does not assert anything. It is only used to obtain integration
                // test data for our router solidity test.
                //
                // Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools
                //
                //         ┌──(USV2)──> WBTC ───(USV2)──> USDC
                //   WETH ─┤
                //         └──(USV2)──> DAI  ───(USV2)──> USDC
                //

                let weth = weth();
                let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
                let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
                let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                let swap_weth_dai = Swap {
                    component: ProtocolComponent {
                        id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: dai.clone(),
                    split: 0.5f64,
                };
                let swap_weth_wbtc = Swap {
                    component: ProtocolComponent {
                        id: "0xBb2b8038a1640196FbE3e38816F3e67Cba72D940".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: wbtc.clone(),
                    // This represents the remaining 50%, but to avoid any rounding errors we set
                    // this to 0 to signify "the remainder of the WETH value".
                    // It should still be very close to 50%
                    split: 0f64,
                };
                let swap_dai_usdc = Swap {
                    component: ProtocolComponent {
                        id: "0xAE461cA67B15dc8dc81CE7615e0320dA1A9aB8D5".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: dai.clone(),
                    token_out: usdc.clone(),
                    split: 0f64,
                };
                let swap_wbtc_usdc = Swap {
                    component: ProtocolComponent {
                        id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(),
                        protocol_system: "uniswap_v2".to_string(),
                        ..Default::default()
                    },
                    token_in: wbtc.clone(),
                    token_out: usdc.clone(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: weth,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: usdc,
                    checked_amount: BigUint::from_str("26173932").unwrap(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap_weth_dai, swap_weth_wbtc, swap_dai_usdc, swap_wbtc_usdc],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth(),
                    Some(get_signer()),
                )
                .unwrap()
                .data;

                let hex_calldata = encode(&calldata);
                write_calldata_to_file("test_split_swap_strategy_encoder", hex_calldata.as_str());
            }

            #[test]
            fn test_split_input_cyclic_swap() {
                // This test has start and end tokens that are the same
                // The flow is:
                //            ┌─ (USV3, 60% split) ──> WETH ─┐
                //            │                              │
                // USDC ──────┤                              ├──(USV2)──> USDC
                //            │                              │
                //            └─ (USV3, 40% split) ──> WETH ─┘

                let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
                let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                // USDC -> WETH (Pool 1) - 60% of input
                let swap_usdc_weth_pool1 = Swap {
                    component: ProtocolComponent {
                        id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(), /* USDC-WETH USV3
                                                                                       * Pool 1 */
                        protocol_system: "uniswap_v3".to_string(),
                        static_attributes: {
                            let mut attrs = HashMap::new();
                            attrs.insert(
                                "fee".to_string(),
                                Bytes::from(BigInt::from(500).to_signed_bytes_be()),
                            );
                            attrs
                        },
                        ..Default::default()
                    },
                    token_in: usdc.clone(),
                    token_out: weth.clone(),
                    split: 0.6f64, // 60% of input
                };

                // USDC -> WETH (Pool 2) - 40% of input (remaining)
                let swap_usdc_weth_pool2 = Swap {
                    component: ProtocolComponent {
                        id: "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8".to_string(), /* USDC-WETH USV3
                                                                                       * Pool 2 */
                        protocol_system: "uniswap_v3".to_string(),
                        static_attributes: {
                            let mut attrs = HashMap::new();
                            attrs.insert(
                                "fee".to_string(),
                                Bytes::from(BigInt::from(3000).to_signed_bytes_be()),
                            );
                            attrs
                        },
                        ..Default::default()
                    },
                    token_in: usdc.clone(),
                    token_out: weth.clone(),
                    split: 0f64, // Remaining 40%
                };

                // WETH -> USDC (Pool 2)
                let swap_weth_usdc_pool2 = Swap {
                    component: ProtocolComponent {
                        id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(), /* USDC-WETH USV2
                                                                                       * Pool 2 */
                        protocol_system: "uniswap_v2".to_string(),
                        static_attributes: {
                            let mut attrs = HashMap::new();
                            attrs.insert(
                                "fee".to_string(),
                                Bytes::from(BigInt::from(3000).to_signed_bytes_be()),
                            );
                            attrs
                        },
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: usdc.clone(),
                    split: 0.0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: usdc.clone(),
                    given_amount: BigUint::from_str("100000000").unwrap(), // 100 USDC (6 decimals)
                    checked_token: usdc.clone(),
                    checked_amount: BigUint::from_str("99574171").unwrap(), /* Expected output
                                                                             * from
                                                                             * test */
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap_usdc_weth_pool1, swap_usdc_weth_pool2, swap_weth_usdc_pool2],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth(),
                    Some(get_signer()),
                )
                .unwrap()
                .data;

                let hex_calldata = hex::encode(&calldata);
                let expected_input = [
                    "7c553846",                                                         // selector
                    "0000000000000000000000000000000000000000000000000000000005f5e100", // given amount
                    "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // given token
                    "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // checked token
                    "0000000000000000000000000000000000000000000000000000000005ef619b", // min amount out
                    "0000000000000000000000000000000000000000000000000000000000000000", // wrap action
                    "0000000000000000000000000000000000000000000000000000000000000000", // unwrap action
                    "0000000000000000000000000000000000000000000000000000000000000002", // tokens length
                    "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                ]
                    .join("");
                let expected_swaps = [
                    "0000000000000000000000000000000000000000000000000000000000000139", // length of ple encoded swaps without padding
                    "006e", // ple encoded swaps
                    "00", // token in index
                    "01", // token out index
                    "999999", // split
                    "2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
                    "0001f4", // pool fee
                    "3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
                    "88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
                    "01", // zero2one
                    "00", // transfer type TransferFrom
                    "006e", // ple encoded swaps
                    "00", // token in index
                    "01", // token out index
                    "000000", // split
                    "2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
                    "000bb8", // pool fee
                    "3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
                    "8ad599c3a0ff1de082011efddc58f1908eb6e6d8", // component id
                    "01", // zero2one
                    "00", // transfer type TransferFrom
                    "0057", // ple encoded swaps
                    "01", // token in index
                    "00", // token out index
                    "000000", // split
                    "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address,
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "b4e16d0168e52d35cacd2c6185b44281ec28c9dc", // component id,
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    "00", // zero2one
                    "01", // transfer type Transfer
                    "00000000000000" // padding
                ]
                    .join("");
                assert_eq!(hex_calldata[..520], expected_input);
                assert_eq!(hex_calldata[1288..], expected_swaps);
                write_calldata_to_file("test_split_input_cyclic_swap", hex_calldata.as_str());
            }

            #[test]
            fn test_split_output_cyclic_swap() {
                // This test has start and end tokens that are the same
                // The flow is:
                //                        ┌─── (USV3, 60% split) ───┐
                //                        │                         │
                // USDC ──(USV2) ── WETH──|                         ├─> USDC
                //                        │                         │
                //                        └─── (USV3, 40% split) ───┘

                let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
                let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                let swap_usdc_weth_v2 = Swap {
                    component: ProtocolComponent {
                        id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(), /* USDC-WETH USV2 */
                        protocol_system: "uniswap_v2".to_string(),
                        static_attributes: {
                            let mut attrs = HashMap::new();
                            attrs.insert(
                                "fee".to_string(),
                                Bytes::from(BigInt::from(500).to_signed_bytes_be()),
                            );
                            attrs
                        },
                        ..Default::default()
                    },
                    token_in: usdc.clone(),
                    token_out: weth.clone(),
                    split: 0.0f64,
                };

                let swap_weth_usdc_v3_pool1 = Swap {
                    component: ProtocolComponent {
                        id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(), /* USDC-WETH USV3
                                                                                       * Pool 1 */
                        protocol_system: "uniswap_v3".to_string(),
                        static_attributes: {
                            let mut attrs = HashMap::new();
                            attrs.insert(
                                "fee".to_string(),
                                Bytes::from(BigInt::from(500).to_signed_bytes_be()),
                            );
                            attrs
                        },
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: usdc.clone(),
                    split: 0.6f64,
                };

                let swap_weth_usdc_v3_pool2 = Swap {
                    component: ProtocolComponent {
                        id: "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8".to_string(), /* USDC-WETH USV3
                                                                                       * Pool 2 */
                        protocol_system: "uniswap_v3".to_string(),
                        static_attributes: {
                            let mut attrs = HashMap::new();
                            attrs.insert(
                                "fee".to_string(),
                                Bytes::from(BigInt::from(3000).to_signed_bytes_be()),
                            );
                            attrs
                        },
                        ..Default::default()
                    },
                    token_in: weth.clone(),
                    token_out: usdc.clone(),
                    split: 0.0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: usdc.clone(),
                    given_amount: BigUint::from_str("100000000").unwrap(), // 100 USDC (6 decimals)
                    checked_token: usdc.clone(),
                    checked_amount: BigUint::from_str("99025908").unwrap(), /* Expected output
                                                                             * from
                                                                             * test */
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![
                        swap_usdc_weth_v2,
                        swap_weth_usdc_v3_pool1,
                        swap_weth_usdc_v3_pool2,
                    ],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth(),
                    Some(get_signer()),
                )
                .unwrap()
                .data;

                let hex_calldata = hex::encode(&calldata);
                let expected_input = [
                    "7c553846",                                                         // selector
                    "0000000000000000000000000000000000000000000000000000000005f5e100", // given amount
                    "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // given token
                    "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // checked token
                    "0000000000000000000000000000000000000000000000000000000005e703f4", // min amount out
                    "0000000000000000000000000000000000000000000000000000000000000000", // wrap action
                    "0000000000000000000000000000000000000000000000000000000000000000", // unwrap action
                    "0000000000000000000000000000000000000000000000000000000000000002", // tokens length
                    "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                ]
                    .join("");

                let expected_swaps = [
                    "0000000000000000000000000000000000000000000000000000000000000139", // length of ple encoded swaps without padding
                    "0057", // ple encoded swaps
                    "00", // token in index
                    "01", // token out index
                    "000000", // split
                    "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
                    "b4e16d0168e52d35cacd2c6185b44281ec28c9dc", // component id
                    "3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
                    "01", // zero2one
                    "00", // transfer type TransferFrom
                    "006e", // ple encoded swaps
                    "01", // token in index
                    "00", // token out index
                    "999999", // split
                    "2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
                    "0001f4", // pool fee
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    "88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
                    "00", // zero2one
                    "01", // transfer type Transfer
                    "006e", // ple encoded swaps
                    "01", // token in index
                    "00", // token out index
                    "000000", // split
                    "2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
                    "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
                    "000bb8", // pool fee
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    "8ad599c3a0ff1de082011efddc58f1908eb6e6d8", // component id
                    "00", // zero2one
                    "01", // transfer type Transfer
                    "00000000000000" // padding
                ]
                    .join("");

                assert_eq!(hex_calldata[..520], expected_input);
                assert_eq!(hex_calldata[1288..], expected_swaps);
                write_calldata_to_file("test_split_output_cyclic_swap", hex_calldata.as_str());
            }
        }

        mod protocol_integration {
            // in this module we test protocol specific logic by creating the calldata that then is
            // used in the solidity tests
            use super::*;

            #[test]
            fn test_single_encoding_strategy_ekubo() {
                //   ETH ──(EKUBO)──> USDC

                let token_in = Bytes::from(Address::ZERO.as_slice());
                let token_out = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC

                let static_attributes = HashMap::from([
                    ("fee".to_string(), Bytes::from(0_u64)),
                    ("tick_spacing".to_string(), Bytes::from(0_u32)),
                    (
                        "extension".to_string(),
                        Bytes::from("0x51d02a5948496a67827242eabc5725531342527c"),
                    ), /* Oracle */
                ]);

                let component = ProtocolComponent {
                    // All Ekubo swaps go through the core contract - not necessary to specify pool
                    // id for test
                    protocol_system: "ekubo_v2".to_string(),
                    static_attributes,
                    ..Default::default()
                };

                let swap = Swap {
                    component,
                    token_in: token_in.clone(),
                    token_out: token_out.clone(),
                    split: 0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                let solution = Solution {
                    exact_out: false,
                    given_token: token_in,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: token_out,
                    checked_amount: BigUint::from_str("1000").unwrap(),
                    // Alice
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFrom,
                    eth(),
                    None,
                )
                .unwrap()
                .data;
                let hex_calldata = encode(&calldata);
                write_calldata_to_file(
                    "test_single_encoding_strategy_ekubo",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_encoding_strategy_maverick() {
                // GHO -> (maverick) -> USDC
                let maverick_pool = ProtocolComponent {
                    id: String::from("0x14Cf6D2Fe3E1B326114b07d22A6F6bb59e346c67"),
                    protocol_system: String::from("vm:maverick_v2"),
                    ..Default::default()
                };
                let token_in = Bytes::from("0x40D16FC0246aD3160Ccc09B8D0D3A2cD28aE6C2f");
                let token_out = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
                let swap = Swap {
                    component: maverick_pool,
                    token_in: token_in.clone(),
                    token_out: token_out.clone(),
                    split: 0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                let solution = Solution {
                    exact_out: false,
                    given_token: token_in,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: token_out,
                    checked_amount: BigUint::from_str("1000").unwrap(),
                    // Alice
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFrom,
                    eth(),
                    None,
                )
                .unwrap()
                .data;
                let hex_calldata = encode(&calldata);
                write_calldata_to_file(
                    "test_single_encoding_strategy_maverick",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_encoding_strategy_usv4_eth_in() {
                // Performs a single swap from ETH to PEPE using a USV4 pool
                // Note: This test does not assert anything. It is only used to obtain integration
                // test data for our router solidity test.
                //
                //   ETH ───(USV4)──> PEPE
                //
                let eth = eth();
                let pepe = Bytes::from_str("0x6982508145454Ce325dDbE47a25d4ec3d2311933").unwrap();

                let pool_fee_eth_pepe = Bytes::from(BigInt::from(25000).to_signed_bytes_be());
                let tick_spacing_eth_pepe = Bytes::from(BigInt::from(500).to_signed_bytes_be());
                let mut static_attributes_eth_pepe: HashMap<String, Bytes> = HashMap::new();
                static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe);
                static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe);

                let swap_eth_pepe = Swap {
                    component: ProtocolComponent {
                        id: "0xecd73ecbf77219f21f129c8836d5d686bbc27d264742ddad620500e3e548e2c9"
                            .to_string(),
                        protocol_system: "uniswap_v4".to_string(),
                        static_attributes: static_attributes_eth_pepe,
                        ..Default::default()
                    },
                    token_in: eth.clone(),
                    token_out: pepe.clone(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: eth.clone(),
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: pepe,
                    checked_amount: BigUint::from_str("152373460199848577067005852").unwrap(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap_eth_pepe],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth,
                    Some(get_signer()),
                )
                .unwrap()
                .data;
                let hex_calldata = encode(&calldata);

                write_calldata_to_file(
                    "test_single_encoding_strategy_usv4_eth_in",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_encoding_strategy_usv4_eth_out() {
                // Performs a single swap from USDC to ETH using a USV4 pool
                // Note: This test does not assert anything. It is only used to obtain integration
                // test data for our router solidity test.
                //
                //   USDC ───(USV4)──> ETH
                //
                let eth = eth();
                let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();

                // Fee and tick spacing information for this test is obtained by querying the
                // USV4 Position Manager contract: 0xbd216513d74c8cf14cf4747e6aaa6420ff64ee9e
                // Using the poolKeys function with the first 25 bytes of the pool id
                let pool_fee_usdc_eth = Bytes::from(BigInt::from(3000).to_signed_bytes_be());
                let tick_spacing_usdc_eth = Bytes::from(BigInt::from(60).to_signed_bytes_be());
                let mut static_attributes_usdc_eth: HashMap<String, Bytes> = HashMap::new();
                static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth);
                static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth);

                let swap_usdc_eth = Swap {
                    component: ProtocolComponent {
                        id: "0xdce6394339af00981949f5f3baf27e3610c76326a700af57e4b3e3ae4977f78d"
                            .to_string(),
                        protocol_system: "uniswap_v4".to_string(),
                        static_attributes: static_attributes_usdc_eth,
                        ..Default::default()
                    },
                    token_in: usdc.clone(),
                    token_out: eth.clone(),
                    split: 0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: usdc,
                    given_amount: BigUint::from_str("3000_000000").unwrap(),
                    checked_token: eth.clone(),
                    checked_amount: BigUint::from_str("1117254495486192350").unwrap(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap_usdc_eth],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth,
                    Some(get_signer()),
                )
                .unwrap()
                .data;

                let hex_calldata = encode(&calldata);
                write_calldata_to_file(
                    "test_single_encoding_strategy_usv4_eth_out",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_encoding_strategy_usv4_grouped_swap() {
                // Performs a sequential swap from USDC to PEPE though ETH using two consecutive
                // USV4 pools
                //
                //   USDC ──(USV4)──> ETH ───(USV4)──> PEPE
                //

                let eth = eth();
                let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
                let pepe = Bytes::from_str("0x6982508145454Ce325dDbE47a25d4ec3d2311933").unwrap();

                // Fee and tick spacing information for this test is obtained by querying the
                // USV4 Position Manager contract: 0xbd216513d74c8cf14cf4747e6aaa6420ff64ee9e
                // Using the poolKeys function with the first 25 bytes of the pool id
                let pool_fee_usdc_eth = Bytes::from(BigInt::from(3000).to_signed_bytes_be());
                let tick_spacing_usdc_eth = Bytes::from(BigInt::from(60).to_signed_bytes_be());
                let mut static_attributes_usdc_eth: HashMap<String, Bytes> = HashMap::new();
                static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth);
                static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth);

                let pool_fee_eth_pepe = Bytes::from(BigInt::from(25000).to_signed_bytes_be());
                let tick_spacing_eth_pepe = Bytes::from(BigInt::from(500).to_signed_bytes_be());
                let mut static_attributes_eth_pepe: HashMap<String, Bytes> = HashMap::new();
                static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe);
                static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe);

                let swap_usdc_eth = Swap {
                    component: ProtocolComponent {
                        id: "0xdce6394339af00981949f5f3baf27e3610c76326a700af57e4b3e3ae4977f78d"
                            .to_string(),
                        protocol_system: "uniswap_v4".to_string(),
                        static_attributes: static_attributes_usdc_eth,
                        ..Default::default()
                    },
                    token_in: usdc.clone(),
                    token_out: eth.clone(),
                    split: 0f64,
                };

                let swap_eth_pepe = Swap {
                    component: ProtocolComponent {
                        id: "0xecd73ecbf77219f21f129c8836d5d686bbc27d264742ddad620500e3e548e2c9"
                            .to_string(),
                        protocol_system: "uniswap_v4".to_string(),
                        static_attributes: static_attributes_eth_pepe,
                        ..Default::default()
                    },
                    token_in: eth.clone(),
                    token_out: pepe.clone(),
                    split: 0f64,
                };
                let encoder = get_tycho_router_encoder(UserTransferType::TransferFromPermit2);

                let solution = Solution {
                    exact_out: false,
                    given_token: usdc,
                    given_amount: BigUint::from_str("1000_000000").unwrap(),
                    checked_token: pepe,
                    checked_amount: BigUint::from_str("97191013220606467325121599").unwrap(),
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap_usdc_eth, swap_eth_pepe],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFromPermit2,
                    eth,
                    Some(get_signer()),
                )
                .unwrap()
                .data;

                let expected_input = [
                    "30ace1b1",                                                              // Function selector (single swap)
                    "000000000000000000000000000000000000000000000000000000003b9aca00",      // amount in
                    "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",      // token in
                    "0000000000000000000000006982508145454ce325ddbe47a25d4ec3d2311933",      // token out
                    "0000000000000000000000000000000000000000005064ff624d54346285543f",      // min amount out
                    "0000000000000000000000000000000000000000000000000000000000000000",      // wrap
                    "0000000000000000000000000000000000000000000000000000000000000000",      // unwrap
                    "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2",      // receiver
                ]
                    .join("");

                // after this there is the permit and because of the deadlines (that depend on block
                // time) it's hard to assert

                let expected_swaps = String::from(concat!(
                    // length of ple encoded swaps without padding
                    "0000000000000000000000000000000000000000000000000000000000000086",
                    // Swap data header
                    "f62849f9a0b5bf2913b396098f7c7019b51a820a", // executor address
                    // Protocol data
                    "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // group token in
                    "6982508145454ce325ddbe47a25d4ec3d2311933", // group token in
                    "00",                                       // zero2one
                    "00",                                       // transfer type TransferFrom
                    "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
                    // First pool params
                    "0000000000000000000000000000000000000000", // intermediary token (ETH)
                    "000bb8",                                   // fee
                    "00003c",                                   // tick spacing
                    // Second pool params
                    "6982508145454ce325ddbe47a25d4ec3d2311933", // intermediary token (PEPE)
                    "0061a8",                                   // fee
                    "0001f4",                                   // tick spacing
                    "0000000000000000000000000000000000000000000000000000"  // padding
                ));

                let hex_calldata = encode(&calldata);

                assert_eq!(hex_calldata[..456], expected_input);
                assert_eq!(hex_calldata[1224..], expected_swaps);
                write_calldata_to_file(
                    "test_single_encoding_strategy_usv4_grouped_swap",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_encoding_strategy_curve() {
                //   UWU ──(curve 2 crypto pool)──> WETH

                let token_in = Bytes::from("0x55C08ca52497e2f1534B59E2917BF524D4765257"); // UWU
                let token_out = Bytes::from("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"); // WETH

                let static_attributes = HashMap::from([(
                    "factory".to_string(),
                    Bytes::from(
                        "0x98ee851a00abee0d95d08cf4ca2bdce32aeaaf7f"
                            .as_bytes()
                            .to_vec(),
                    )),
                    ("coins".to_string(), Bytes::from_str("0x5b22307863303261616133396232323366653864306130653563346632376561643930383363373536636332222c22307835356330386361353234393765326631353334623539653239313762663532346434373635323537225d").unwrap()),
                ]);

                let component = ProtocolComponent {
                    id: String::from("0x77146B0a1d08B6844376dF6d9da99bA7F1b19e71"),
                    protocol_system: String::from("vm:curve"),
                    static_attributes,
                    ..Default::default()
                };

                let swap = Swap {
                    component,
                    token_in: token_in.clone(),
                    token_out: token_out.clone(),
                    split: 0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                let solution = Solution {
                    exact_out: false,
                    given_token: token_in,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: token_out,
                    checked_amount: BigUint::from_str("1").unwrap(),
                    // Alice
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFrom,
                    eth(),
                    None,
                )
                .unwrap()
                .data;

                let hex_calldata = encode(&calldata);
                write_calldata_to_file(
                    "test_single_encoding_strategy_curve",
                    hex_calldata.as_str(),
                );
            }

            #[test]
            fn test_single_encoding_strategy_curve_st_eth() {
                //   ETH ──(curve stETH pool)──> STETH

                let token_in = Bytes::from("0x0000000000000000000000000000000000000000"); // ETH
                let token_out = Bytes::from("0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84"); // STETH

                let static_attributes = HashMap::from([(
                    "factory".to_string(),
                    Bytes::from(
                        "0x0000000000000000000000000000000000000000"
                            .as_bytes()
                            .to_vec(),
                    ),
                ),
                    ("coins".to_string(), Bytes::from_str("0x5b22307865656565656565656565656565656565656565656565656565656565656565656565656565656565222c22307861653761623936353230646533613138653565313131623565616162303935333132643766653834225d").unwrap()),]);

                let component = ProtocolComponent {
                    id: String::from("0xDC24316b9AE028F1497c275EB9192a3Ea0f67022"),
                    protocol_system: String::from("vm:curve"),
                    static_attributes,
                    ..Default::default()
                };

                let swap = Swap {
                    component,
                    token_in: token_in.clone(),
                    token_out: token_out.clone(),
                    split: 0f64,
                };

                let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);

                let solution = Solution {
                    exact_out: false,
                    given_token: token_in,
                    given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
                    checked_token: token_out,
                    checked_amount: BigUint::from_str("1").unwrap(),
                    // Alice
                    sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
                    receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
                        .unwrap(),
                    swaps: vec![swap],
                    ..Default::default()
                };

                let encoded_solution = encoder
                    .encode_solutions(vec![solution.clone()])
                    .unwrap()[0]
                    .clone();

                let calldata = encode_tycho_router_call(
                    eth_chain().id,
                    encoded_solution,
                    &solution,
                    UserTransferType::TransferFrom,
                    eth(),
                    None,
                )
                .unwrap()
                .data;

                let hex_calldata = encode(&calldata);
                write_calldata_to_file(
                    "test_single_encoding_strategy_curve_st_eth",
                    hex_calldata.as_str(),
                );
            }
        }
    }
}
