diff --git a/Cargo.lock b/Cargo.lock index 364e529..46cd809 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4317,8 +4317,8 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "tycho-core" -version = "0.55.2" -source = "git+https://github.com/propeller-heads/tycho-indexer.git?tag=0.55.2#dfa50d5e318253001938655a49aa3e05f958d89e" +version = "0.56.5" +source = "git+https://github.com/propeller-heads/tycho-indexer.git?tag=0.56.5#2af8c1a5a61c5479eab5f7903b69943efa61e2c8" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 64ce4e5..94c6602 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ clap = { version = "4.5.3", features = ["derive"] } alloy = { version = "0.9.2", features = ["providers", "rpc-types-eth", "eip712", "signer-local"], optional = true } alloy-sol-types = { version = "0.8.14", optional = true } alloy-primitives = { version = "0.8.9", optional = true } -tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.55.2" } +tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.56.5" } once_cell = "1.20.2" [dev-dependencies] diff --git a/README.md b/README.md index ada92e4..1c0dbd2 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,18 @@ The command lets you choose the encoding strategy to be used. The available stra #### Tycho Router -`tycho-router`: Encodes a transaction using the Tycho Router encoding strategy. Requires a private key for signing +`tycho-router`: Encodes a transaction using the Tycho Router encoding strategy. + +Example: + +```bash +echo '' | tycho-encode tycho-router +``` + +#### Tycho Router With Permit2 + +`tycho-router-with-permit2`: Encodes a transaction using the Tycho Router encoding strategy. Requires a private key for +signing Permit2. Example: @@ -64,7 +75,7 @@ The commands accept the following options: Here's a complete example that encodes a swap from WETH to DAI using Uniswap V2 and the Tycho Router strategy: ```bash -echo '{"sender":"0x1234567890123456789012345678901234567890","receiver":"0x1234567890123456789012345678901234567890","given_token":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","given_amount":"1000000000000000000","checked_token":"0x6B175474E89094C44Da98b954EedeAC495271d0F","exact_out":false,"slippage":0.01,"expected_amount":"1000000000000000000","checked_amount":"990000000000000000","router_address":"0xaa820C29648D5EA543d712cC928377Bd7206a0E7","swaps":[{"component":{"id":"0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640","protocol_system":"uniswap_v2","protocol_type_name":"UniswapV2Pool","chain":"ethereum","tokens":["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"],"contract_ids":["0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"],"static_attributes":{"factory":"0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f"},"change":"Update","creation_tx":"0x0000000000000000000000000000000000000000000000000000000000000000","created_at":"2024-02-28T12:00:00"},"token_in":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","token_out":"0x6B175474E89094C44Da98b954EedeAC495271d0F","split":0.0}],"direct_execution":true}' | tycho-encode tycho-router --swapper-pk 0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234 +echo '{"sender":"0x1234567890123456789012345678901234567890","receiver":"0x1234567890123456789012345678901234567890","given_token":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","given_amount":"1000000000000000000","checked_token":"0x6B175474E89094C44Da98b954EedeAC495271d0F","exact_out":false,"slippage":0.01,"expected_amount":"1000000000000000000","checked_amount":"990000000000000000","router_address":"0xaa820C29648D5EA543d712cC928377Bd7206a0E7","swaps":[{"component":{"id":"0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640","protocol_system":"uniswap_v2","protocol_type_name":"UniswapV2Pool","chain":"ethereum","tokens":["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"],"contract_ids":["0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"],"static_attributes":{"factory":"0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f"},"change":"Update","creation_tx":"0x0000000000000000000000000000000000000000000000000000000000000000","created_at":"2024-02-28T12:00:00"},"token_in":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","token_out":"0x6B175474E89094C44Da98b954EedeAC495271d0F","split":0.0}],"direct_execution":true}' | tycho-encode tycho-router-permit2 --swapper-pk 0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234 ``` #### JSON Payload Structure: Solution struct diff --git a/examples/quickstart/main.rs b/examples/quickstart/main.rs index d74cd9a..7c0ca51 100644 --- a/examples/quickstart/main.rs +++ b/examples/quickstart/main.rs @@ -2,7 +2,7 @@ use std::str::FromStr; use num_bigint::BigUint; use tycho_core::{ - models::{protocol::ProtocolComponent, Chain as TychoCoreChain}, + models::{protocol::ProtocolComponent, Chain}, Bytes, }; use tycho_execution::encoding::{ @@ -22,8 +22,8 @@ fn main() { // Initialize the encoder let encoder = EVMEncoderBuilder::new() - .chain(TychoCoreChain::Ethereum) - .tycho_router(swapper_pk, None) + .chain(Chain::Ethereum) + .tycho_router_with_permit2(None, swapper_pk) .expect("Failed to create encoder builder") .build() .expect("Failed to build encoder"); diff --git a/foundry/test/TychoRouter.t.sol b/foundry/test/TychoRouter.t.sol index e31f082..ccfb7ea 100644 --- a/foundry/test/TychoRouter.t.sol +++ b/foundry/test/TychoRouter.t.sol @@ -710,7 +710,7 @@ contract TychoRouterTest is TychoRouterTestSetup { uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertGt(balancerAfter - balancerBefore, 26173932); + assertEq(balancerAfter - balancerBefore, 2659881924818443699787); } function testSingleSwapWithoutPermit2Integration() public { @@ -720,17 +720,18 @@ contract TychoRouterTest is TychoRouterTestSetup { // Tests swapping WETH -> DAI on a USV2 pool without permit2 deal(WETH_ADDR, tychoRouterAddr, 1 ether); uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE); + // Encoded solution generated using `test_split_swap_strategy_encoder_simple_route_no_permit2` + // but manually replacing the executor address + // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test + // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` + (bool success,) = tychoRouterAddr.call( + hex"0a83cb080000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000008f1d5c1cae37400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000" + ); - // TODO replace this calldata once we have the encoder side - // (bool success,) = tychoRouterAddr.call( - // hex"d499aa880000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d481bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfbc3000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041f2740fde9662d8bc1f8fe8e8fc29447c1832d625f06f4a56ee5103ad555c12323af5d50eb840f73d17873383ae3b7573956d5df7b2bf76bddba768c2837894a51b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000" - // ); - // - // vm.stopPrank(); - // - // uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE); - // assertTrue(success, "Call Failed"); - // assertGt(balancerAfter - balancerBefore, 26173932); + vm.stopPrank(); + uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE); + assertTrue(success, "Call Failed"); + assertEq(balancerAfter - balancerBefore, 2659881924818443699787); } function testUSV4Integration() public { @@ -790,7 +791,7 @@ contract TychoRouterTest is TychoRouterTestSetup { uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertGt(balancerAfter - balancerBefore, 26173932); + assertEq(balancerAfter - balancerBefore, 2659881924818443699787); } function testSingleSwapWithUnwrapIntegration() public { @@ -819,7 +820,7 @@ contract TychoRouterTest is TychoRouterTestSetup { uint256 balancerAfter = ALICE.balance; assertTrue(success, "Call Failed"); - assertGt(balancerAfter - balancerBefore, 26173932); + assertEq(balancerAfter - balancerBefore, 1120007305574805922); } function testSplitSwapIntegration() public { diff --git a/src/bin/tycho-encode.rs b/src/bin/tycho-encode.rs index 96f9387..2178269 100644 --- a/src/bin/tycho-encode.rs +++ b/src/bin/tycho-encode.rs @@ -1,11 +1,9 @@ use std::io::{self, Read}; use clap::{Parser, Subcommand}; -use serde_json::Value; use tycho_core::models::Chain; use tycho_execution::encoding::{ - errors::EncodingError, evm::encoder_builder::EVMEncoderBuilder, models::Solution, - tycho_encoder::TychoEncoder, + evm::encoder_builder::EVMEncoderBuilder, models::Solution, tycho_encoder::TychoEncoder, }; #[derive(Parser)] @@ -52,6 +50,11 @@ pub enum Commands { TychoRouter { #[arg(short, long)] config_path: Option, + }, + /// Use the Tycho router encoding strategy with Permit2 approval and token in transfer + TychoRouterPermit2 { + #[arg(short, long)] + config_path: Option, #[arg(short, long)] swapper_pk: String, }, @@ -64,6 +67,7 @@ pub enum Commands { fn main() -> Result<(), Box> { let cli = Cli::parse(); + let chain = Chain::Ethereum; // Read from stdin until EOF let mut buffer = String::new(); @@ -74,16 +78,24 @@ fn main() -> Result<(), Box> { if buffer.trim().is_empty() { return Err("No input provided. Expected JSON input on stdin.".into()); } + let solution: Solution = serde_json::from_str(&buffer)?; - // Encode the solution - let encoded = match cli.command { - Commands::TychoRouter { config_path, swapper_pk } => { - encode_swaps(&buffer, config_path, Some(swapper_pk), true)? - } - Commands::DirectExecution { config_path } => { - encode_swaps(&buffer, config_path, None, false)? + let mut builder = EVMEncoderBuilder::new().chain(chain); + + builder = match cli.command { + Commands::TychoRouter { config_path } => builder.tycho_router(config_path)?, + Commands::TychoRouterPermit2 { config_path, swapper_pk } => { + builder.tycho_router_with_permit2(config_path, swapper_pk)? } + Commands::DirectExecution { config_path } => builder.direct_execution(config_path)?, }; + let encoder = builder.build()?; + let transactions = encoder.encode_router_calldata(vec![solution])?; + let encoded = serde_json::json!({ + "to": format!("0x{}", hex::encode(&transactions[0].to)), + "value": format!("0x{}", hex::encode(transactions[0].value.to_bytes_be())), + "data": format!("0x{}", hex::encode(&transactions[0].data)), + }); // Output the encoded result as JSON to stdout println!( "{}", @@ -93,32 +105,3 @@ fn main() -> Result<(), Box> { Ok(()) } - -fn encode_swaps( - input: &str, - config_path: Option, - swapper_pk: Option, - use_tycho_router: bool, -) -> Result { - let solution: Solution = serde_json::from_str(input)?; - let chain = Chain::Ethereum; - - let mut builder = EVMEncoderBuilder::new().chain(chain); - builder = if use_tycho_router { - let private_key = swapper_pk.ok_or(EncodingError::FatalError( - "Swapper private key is required for tycho_router".to_string(), - ))?; - builder.tycho_router(private_key, config_path)? - } else { - builder.direct_execution(config_path)? - }; - let encoder = builder.build()?; - - let transactions = encoder.encode_router_calldata(vec![solution])?; - - Ok(serde_json::json!({ - "to": format!("0x{}", hex::encode(&transactions[0].to)), - "value": format!("0x{}", hex::encode(transactions[0].value.to_bytes_be())), - "data": format!("0x{}", hex::encode(&transactions[0].data)), - })) -} diff --git a/src/encoding/evm/encoder_builder.rs b/src/encoding/evm/encoder_builder.rs index eb9011f..f7dfa56 100644 --- a/src/encoding/evm/encoder_builder.rs +++ b/src/encoding/evm/encoder_builder.rs @@ -42,17 +42,35 @@ impl EVMEncoderBuilder { self } - /// Shortcut method to initialize a `SplitSwapStrategyEncoder` with a given `swapper_pk`. - /// **Note**: Should not be used at the same time as `strategy_encoder`. - pub fn tycho_router( - self, - swapper_pk: String, - executors_file_path: Option, - ) -> Result { + /// Shortcut method to initialize a `SplitSwapStrategyEncoder` without any approval nor token in + /// transfer. **Note**: Should not be used at the same time as `strategy_encoder`. + pub fn tycho_router(self, executors_file_path: Option) -> Result { if let Some(chain) = self.chain { let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?; let strategy = - Box::new(SplitSwapStrategyEncoder::new(swapper_pk, chain, swap_encoder_registry)?); + Box::new(SplitSwapStrategyEncoder::new(chain, swap_encoder_registry, None)?); + Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) }) + } else { + Err(EncodingError::FatalError( + "Please set the chain before setting the tycho router".to_string(), + )) + } + } + + /// Shortcut method to initialize a `SplitSwapStrategyEncoder` with Permit2 approval and token + /// in transfer. **Note**: Should not be used at the same time as `strategy_encoder`. + pub fn tycho_router_with_permit2( + self, + executors_file_path: Option, + swapper_pk: String, + ) -> Result { + if let Some(chain) = self.chain { + let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?; + let strategy = Box::new(SplitSwapStrategyEncoder::new( + chain, + swap_encoder_registry, + Some(swapper_pk), + )?); Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) }) } else { Err(EncodingError::FatalError( diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index 6983268..3749e0f 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -79,7 +79,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder { #[derive(Clone)] pub struct SplitSwapStrategyEncoder { swap_encoder_registry: SwapEncoderRegistry, - permit2: Permit2, + permit2: Option, selector: String, native_address: Bytes, wrapped_address: Bytes, @@ -88,14 +88,21 @@ pub struct SplitSwapStrategyEncoder { impl SplitSwapStrategyEncoder { pub fn new( - swapper_pk: String, blockchain: tycho_core::models::Chain, swap_encoder_registry: SwapEncoderRegistry, + swapper_pk: Option, ) -> Result { let chain = Chain::from(blockchain); - let selector = "swapPermit2(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string(); + let (permit2, selector) = if let Some(swapper_pk) = swapper_pk { + (Some(Permit2::new(swapper_pk, chain.clone())?), "swapPermit2(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string()) + } else { + ( + None, + "swap(uint256,address,address,uint256,bool,bool,uint256,address,bytes)".to_string(), + ) + }; Ok(Self { - permit2: Permit2::new(swapper_pk, chain.clone())?, + permit2, selector, swap_encoder_registry, native_address: chain.native_token()?, @@ -122,12 +129,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { &self.native_address, &self.wrapped_address, )?; - let (permit, signature) = self.permit2.get_permit( - &solution.router_address, - &solution.sender, - &solution.given_token, - &solution.given_amount, - )?; + let min_amount_out = get_min_amount_for_solution(solution.clone()); // The tokens array is composed of the given token, the checked token and all the @@ -214,20 +216,41 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { } let encoded_swaps = self.ple_encode(swaps); - let method_calldata = ( - biguint_to_u256(&solution.given_amount), - bytes_to_address(&solution.given_token)?, - bytes_to_address(&solution.checked_token)?, - biguint_to_u256(&min_amount_out), - wrap, - unwrap, - U256::from(tokens.len()), - bytes_to_address(&solution.receiver)?, - permit, - signature.as_bytes().to_vec(), - encoded_swaps, - ) - .abi_encode(); + let method_calldata = if let Some(permit2) = self.permit2.clone() { + let (permit, signature) = permit2.get_permit( + &solution.router_address, + &solution.sender, + &solution.given_token, + &solution.given_amount, + )?; + ( + biguint_to_u256(&solution.given_amount), + bytes_to_address(&solution.given_token)?, + bytes_to_address(&solution.checked_token)?, + biguint_to_u256(&min_amount_out), + wrap, + unwrap, + U256::from(tokens.len()), + bytes_to_address(&solution.receiver)?, + permit, + signature.as_bytes().to_vec(), + encoded_swaps, + ) + .abi_encode() + } else { + ( + biguint_to_u256(&solution.given_amount), + bytes_to_address(&solution.given_token)?, + bytes_to_address(&solution.checked_token)?, + biguint_to_u256(&min_amount_out), + wrap, + unwrap, + U256::from(tokens.len()), + bytes_to_address(&solution.receiver)?, + encoded_swaps, + ) + .abi_encode() + }; let contract_interaction = encode_input(&self.selector, method_calldata); Ok((contract_interaction, solution.router_address, None)) @@ -606,7 +629,8 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); + SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key)) + .unwrap(); let solution = Solution { exact_out: false, given_token: weth, @@ -707,7 +731,8 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); + SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key)) + .unwrap(); let solution = Solution { exact_out: false, given_token: eth(), @@ -755,7 +780,8 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); + SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key)) + .unwrap(); let solution = Solution { exact_out: false, given_token: dai, @@ -844,7 +870,8 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); + SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key)) + .unwrap(); let solution = Solution { exact_out: false, given_token: weth, @@ -924,7 +951,8 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); + SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key)) + .unwrap(); let solution = Solution { exact_out: false, given_token: usdc, @@ -1010,4 +1038,84 @@ mod tests { assert_eq!(hex_calldata[..520], expected_input); assert_eq!(hex_calldata[1288..], expected_swaps); } + + #[test] + fn test_split_swap_strategy_encoder_simple_route_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 expected_amount = Some(BigUint::from_str("2_650_000000000000000000").unwrap()); + let slippage = Some(0.01f64); + let checked_amount = Some(BigUint::from_str("2_640_000000000000000000").unwrap()); + let expected_min_amount = U256::from_str("2_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 swap_encoder_registry = get_swap_encoder_registry(); + let encoder = + SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, None).unwrap(); + let solution = Solution { + exact_out: false, + given_token: weth, + given_amount: BigUint::from_str("1_000000000000000000").unwrap(), + checked_token: dai, + expected_amount, + slippage, + checked_amount, + sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), + swaps: vec![swap], + ..Default::default() + }; + + let (calldata, _, _) = encoder + .encode_strategy(solution) + .unwrap(); + let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount)); + let expected_input = [ + "0a83cb08", // Function selector + "0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount out + "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in + "0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f", // token out + &expected_min_amount_encoded, // min amount out + "0000000000000000000000000000000000000000000000000000000000000000", // wrap + "0000000000000000000000000000000000000000000000000000000000000000", // unwrap + "0000000000000000000000000000000000000000000000000000000000000002", // tokens length + "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver + "0000000000000000000000000000000000000000000000000000000000000120", // offset of ple encoded swaps + "000000000000000000000000000000000000000000000000000000000000005c", // length of ple encoded swaps without padding + "005a", // ple encoded swaps + // Swap header + "00", // token in index + "01", // token out index + "000000", // split + // Swap data + "5c2f5a71f67c01775180adc06909288b4c329308", // executor address + "bd0625ab", // selector + "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in + "a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id + "3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver + "00", // zero2one + "00", // exact out + "000000", // padding + ] + .join(""); + + let hex_calldata = encode(&calldata); + + assert_eq!(hex_calldata, expected_input); + println!("{}", hex_calldata); + } } diff --git a/src/encoding/evm/tycho_encoder.rs b/src/encoding/evm/tycho_encoder.rs index aef79b7..cfd4375 100644 --- a/src/encoding/evm/tycho_encoder.rs +++ b/src/encoding/evm/tycho_encoder.rs @@ -20,6 +20,16 @@ pub struct EVMTychoEncoder { wrapped_address: Bytes, } +impl Clone for EVMTychoEncoder { + fn clone(&self) -> Self { + Self { + strategy_encoder: self.strategy_encoder.clone_box(), + native_address: self.native_address.clone(), + wrapped_address: self.wrapped_address.clone(), + } + } +} + impl EVMTychoEncoder { pub fn new( chain: tycho_core::models::Chain,