From 5c396512cf695dab3b0d0fec16f71b916661d54d Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 21:20:22 +0530 Subject: [PATCH 01/16] feat: update ExecutorEncoder interface and relevant types --- src/encoding/evm/router_encoder.rs | 11 ++++++++--- src/encoding/evm/strategy_encoder/encoder.rs | 19 +++++++++++-------- src/encoding/evm/strategy_encoder/selector.rs | 4 ++-- src/encoding/models.rs | 3 +++ src/encoding/strategy_encoder.rs | 4 +++- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/encoding/evm/router_encoder.rs b/src/encoding/evm/router_encoder.rs index 53ab437..a564e3e 100644 --- a/src/encoding/evm/router_encoder.rs +++ b/src/encoding/evm/router_encoder.rs @@ -30,7 +30,7 @@ impl RouterEncoder for EVMRo &self, solutions: Vec, ) -> Result, EncodingError> { - let _approvals_calldata = self.handle_approvals(&solutions)?; // TODO: where should we append this? + let _approvals_calldata = self.handle_approvals(&solutions)?; let mut transactions: Vec = Vec::new(); for solution in solutions.iter() { let exact_out = solution.exact_out; @@ -39,7 +39,8 @@ impl RouterEncoder for EVMRo let strategy = self .strategy_selector .select_strategy(solution); - let method_calldata = strategy.encode_strategy((*solution).clone())?; + let (method_calldata, target_address) = + strategy.encode_strategy((*solution).clone())?; let contract_interaction = if straight_to_pool { method_calldata @@ -52,7 +53,11 @@ impl RouterEncoder for EVMRo } else { BigUint::ZERO }; - transactions.push(Transaction { value, data: contract_interaction }); + transactions.push(Transaction { + value, + data: contract_interaction, + to: target_address, + }); } Ok(transactions) } diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index 7068608..35c1b19 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use alloy_primitives::Address; use alloy_sol_types::SolValue; @@ -27,7 +29,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder { pub struct SplitSwapStrategyEncoder {} impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} impl StrategyEncoder for SplitSwapStrategyEncoder { - fn encode_strategy(&self, _solution: Solution) -> Result, EncodingError> { + fn encode_strategy(&self, _solution: Solution) -> Result<(Vec, Address), EncodingError> { todo!() } fn selector(&self, _exact_out: bool) -> &str { @@ -37,10 +39,10 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { /// This strategy encoder is used for solutions that are sent directly to the pool. /// Only 1 solution with 1 swap is supported. -pub struct StraightToPoolStrategyEncoder {} -impl EVMStrategyEncoder for StraightToPoolStrategyEncoder {} -impl StrategyEncoder for StraightToPoolStrategyEncoder { - fn encode_strategy(&self, solution: Solution) -> Result, EncodingError> { +pub struct ExecutorEncoder {} +impl EVMStrategyEncoder for ExecutorEncoder {} +impl StrategyEncoder for ExecutorEncoder { + fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Address), EncodingError> { if solution.router_address.is_none() { return Err(EncodingError::InvalidInput( "Router address is required for straight to pool solutions".to_string(), @@ -68,10 +70,11 @@ impl StrategyEncoder for StraightToPoolStrategyEncoder { router_address, }; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?; - // TODO: here we need to pass also the address of the executor to be used - Ok(protocol_data) + let executor_address = Address::from_str(swap_encoder.executor_address()) + .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?; + Ok((protocol_data, executor_address)) } fn selector(&self, _exact_out: bool) -> &str { - unimplemented!(); + "swap(uint256, bytes)" } } diff --git a/src/encoding/evm/strategy_encoder/selector.rs b/src/encoding/evm/strategy_encoder/selector.rs index 37e397c..5ede40a 100644 --- a/src/encoding/evm/strategy_encoder/selector.rs +++ b/src/encoding/evm/strategy_encoder/selector.rs @@ -1,5 +1,5 @@ use crate::encoding::{ - evm::strategy_encoder::encoder::{SplitSwapStrategyEncoder, StraightToPoolStrategyEncoder}, + evm::strategy_encoder::encoder::{ExecutorEncoder, SplitSwapStrategyEncoder}, models::Solution, strategy_encoder::{StrategyEncoder, StrategySelector}, }; @@ -9,7 +9,7 @@ pub struct EVMStrategySelector; impl StrategySelector for EVMStrategySelector { fn select_strategy(&self, solution: &Solution) -> Box { if solution.straight_to_pool { - Box::new(StraightToPoolStrategyEncoder {}) + Box::new(ExecutorEncoder {}) } else { Box::new(SplitSwapStrategyEncoder {}) } diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 4e2cbbd..431cdc5 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -1,3 +1,4 @@ +use alloy_primitives::Address; use num_bigint::BigUint; use tycho_core::{dto::ProtocolComponent, Bytes}; @@ -60,6 +61,8 @@ pub struct Transaction { pub data: Vec, // ETH value to be sent with the transaction. pub value: BigUint, + // Address of the contract to call with the calldata + pub to: Address, } #[allow(dead_code)] diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index 2b4e348..fb3aa41 100644 --- a/src/encoding/strategy_encoder.rs +++ b/src/encoding/strategy_encoder.rs @@ -1,8 +1,10 @@ +use alloy_primitives::Address; + use crate::encoding::{errors::EncodingError, models::Solution}; #[allow(dead_code)] pub trait StrategyEncoder { - fn encode_strategy(&self, to_encode: Solution) -> Result, EncodingError>; + fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec, Address), EncodingError>; fn selector(&self, exact_out: bool) -> &str; } From ad70a0d5a87f2a89d78de7f4ae783f6c80097407 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 22:26:24 +0530 Subject: [PATCH 02/16] feat: add executor encoder test --- Cargo.lock | 4 +- src/encoding/evm/strategy_encoder/encoder.rs | 55 ++++++++++++++++++++ src/encoding/models.rs | 2 +- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2315882..378aa43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2941,7 +2941,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "157c5a9d7ea5c2ed2d9fb8f495b64759f7816c7eaea54ba3978f0d63000162e3" dependencies = [ "anyhow", - "itertools 0.10.5", + "itertools 0.13.0", "proc-macro2", "quote", "syn 2.0.96", @@ -3011,7 +3011,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index 35c1b19..fb71261 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -78,3 +78,58 @@ impl StrategyEncoder for ExecutorEncoder { "swap(uint256, bytes)" } } + +#[cfg(test)] +mod tests { + use num_bigint::BigUint; + use tycho_core::{dto::ProtocolComponent, Bytes}; + + use super::*; + use crate::encoding::models::Swap; + + #[test] + fn test_executor_encoder() { + let encoder = ExecutorEncoder {}; + + let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"); + let token_out = Bytes::from("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"); + + let swap = Swap { + component: ProtocolComponent { + id: "0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014" + .to_string(), + protocol_system: "vm:balancer_v2".to_string(), + ..Default::default() + }, + token_in: token_in.clone(), + token_out: token_out.clone(), + split: 0.5, + }; + + let solution = Solution { + exact_out: false, + given_token: token_in, + given_amount: BigUint::from(1000000000000000000u64), + expected_amount: BigUint::from(1000000000000000000u64), + checked_token: token_out, + check_amount: None, + sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(), + receiver: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(), + swaps: vec![swap], + straight_to_pool: true, + router_address: Some( + Bytes::from_str("0x0000000000000000000000000000000000000002").unwrap(), + ), + slippage: None, + native_action: None, + }; + + let (_, executor_address) = encoder + .encode_strategy(solution) + .unwrap(); + assert_eq!( + executor_address, + Address::from_str("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4").unwrap() + ); + } +} diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 431cdc5..7a93105 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -12,7 +12,7 @@ pub struct Solution { /// Amount of the given token. pub given_amount: BigUint, /// The token being bought (exact in) or sold (exact out). - checked_token: Bytes, + pub checked_token: Bytes, /// Expected amount of the bought token (exact in) or sold token (exact out). pub expected_amount: BigUint, /// Minimum amount to be checked for the solution to be valid. From d2289c3765ee320f3116f60aa5a9f367be398e3c Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 22:41:11 +0530 Subject: [PATCH 03/16] chore: update test --- src/encoding/evm/strategy_encoder/encoder.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index fb71261..1bb8c6f 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -92,18 +92,17 @@ mod tests { let encoder = ExecutorEncoder {}; let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"); - let token_out = Bytes::from("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"); + let token_out = Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f"); let swap = Swap { component: ProtocolComponent { - id: "0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014" - .to_string(), - protocol_system: "vm:balancer_v2".to_string(), + id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(), + protocol_system: "uniswap_v2".to_string(), ..Default::default() }, token_in: token_in.clone(), token_out: token_out.clone(), - split: 0.5, + split: 0f64, }; let solution = Solution { @@ -114,12 +113,10 @@ mod tests { checked_token: token_out, check_amount: None, sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(), - receiver: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(), + receiver: Bytes::from_str("0x0000000000000000000000000000000000000001").unwrap(), swaps: vec![swap], straight_to_pool: true, - router_address: Some( - Bytes::from_str("0x0000000000000000000000000000000000000002").unwrap(), - ), + router_address: Some(Bytes::zero(20)), slippage: None, native_action: None, }; @@ -129,7 +126,7 @@ mod tests { .unwrap(); assert_eq!( executor_address, - Address::from_str("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4").unwrap() + Address::from_str("0x5c2f5a71f67c01775180adc06909288b4c329308").unwrap() ); } } From 0007c4924ccb7a3f50f127ce25c3f1c7c27c8caf Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 22:50:10 +0530 Subject: [PATCH 04/16] chore: update executor encoder tests --- src/encoding/evm/strategy_encoder/encoder.rs | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index 1bb8c6f..644bd67 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -81,6 +81,7 @@ impl StrategyEncoder for ExecutorEncoder { #[cfg(test)] mod tests { + use alloy::hex::encode; use num_bigint::BigUint; use tycho_core::{dto::ProtocolComponent, Bytes}; @@ -121,12 +122,34 @@ mod tests { native_action: None, }; - let (_, executor_address) = encoder + let (protocol_data, executor_address) = encoder .encode_strategy(solution) .unwrap(); + let hex_protocol_data = encode(&protocol_data); assert_eq!( executor_address, Address::from_str("0x5c2f5a71f67c01775180adc06909288b4c329308").unwrap() ); + assert_eq!( + hex_protocol_data, + String::from(concat!( + // in token + "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", + // component id + "88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", + // receiver + "0000000000000000000000000000000000000001", + // zero for one + "00", + // exact out + "00", + )) + ); + } + + #[test] + fn test_selector() { + let encoder = ExecutorEncoder {}; + assert_eq!(encoder.selector(false), "swap(uint256, bytes)"); } } From c482e21a5f7b254a19cca53c5a86b97830a90932 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 23:10:23 +0530 Subject: [PATCH 05/16] feat: add univ2 executor test with hex --- foundry/src/executors/UniswapV2Executor.sol | 2 +- foundry/test/executors/UniswapV2Executor.t.sol | 16 +++++++++++++++- src/encoding/evm/strategy_encoder/encoder.rs | 1 + 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/foundry/src/executors/UniswapV2Executor.sol b/foundry/src/executors/UniswapV2Executor.sol index 9f447f1..16bef82 100644 --- a/foundry/src/executors/UniswapV2Executor.sol +++ b/foundry/src/executors/UniswapV2Executor.sol @@ -43,7 +43,7 @@ contract UniswapV2Executor is IExecutor { bool zeroForOne ) { - if (data.length != 61) { + if (data.length < 61) { revert UniswapV2Executor__InvalidDataLength(); } inToken = IERC20(address(bytes20(data[0:20]))); diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index cdc0c89..300293c 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.28; import "@src/executors/UniswapV2Executor.sol"; import {Test} from "../../lib/forge-std/src/Test.sol"; import {Constants} from "../Constants.sol"; - +import {console} from "forge-std/console.sol"; contract UniswapV2ExecutorExposed is UniswapV2Executor { function decodeParams(bytes calldata data) external @@ -93,4 +93,18 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { uint256 finalBalance = DAI.balanceOf(BOB); assertGe(finalBalance, amountOut); } + + function testSwapExecutorEncoderData() public { + bytes memory protocolData = + hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f564000000000000000000000000000000000000000010000"; + console.log(protocolData.length); + + (IERC20 tokenIn, address target, address receiver, bool zeroForOne) = + uniswapV2Exposed.decodeParams(protocolData); + + assertEq(address(tokenIn), WETH_ADDR); + assertEq(target, 0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640); + assertEq(receiver, 0x0000000000000000000000000000000000000001); + assertEq(zeroForOne, false); + } } diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index 644bd67..87b8122 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -126,6 +126,7 @@ mod tests { .encode_strategy(solution) .unwrap(); let hex_protocol_data = encode(&protocol_data); + println!("{}", hex_protocol_data); assert_eq!( executor_address, Address::from_str("0x5c2f5a71f67c01775180adc06909288b4c329308").unwrap() From 0196767eff1d18481e3154defd92514bd45d74b9 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 23:20:55 +0530 Subject: [PATCH 06/16] feat: add swap test with hex for univ2 executor --- foundry/test/executors/UniswapV2Executor.t.sol | 12 ++++++++++++ src/encoding/evm/strategy_encoder/encoder.rs | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index 300293c..070bc21 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -107,4 +107,16 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { assertEq(receiver, 0x0000000000000000000000000000000000000001); assertEq(zeroForOne, false); } + + function testSwapExecutorSwap() public { + bytes memory protocolData = + hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb111d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e0000"; + uint256 amountIn = 10 ** 18; + uint256 amountOut = 1847751195973566072891; + deal(WETH_ADDR, address(uniswapV2Exposed), amountIn); + uniswapV2Exposed.swap(amountIn, protocolData); + + uint256 finalBalance = DAI.balanceOf(BOB); + assertGe(finalBalance, amountOut); + } } diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index 87b8122..b12a50e 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -97,7 +97,7 @@ mod tests { let swap = Swap { component: ProtocolComponent { - id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(), + id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(), protocol_system: "uniswap_v2".to_string(), ..Default::default() }, @@ -114,7 +114,7 @@ mod tests { checked_token: token_out, check_amount: None, sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(), - receiver: Bytes::from_str("0x0000000000000000000000000000000000000001").unwrap(), + receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(), swaps: vec![swap], straight_to_pool: true, router_address: Some(Bytes::zero(20)), @@ -137,9 +137,9 @@ mod tests { // in token "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // component id - "88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", + "a478c2975ab1ea89e8196811f51a7b7ade33eb11", // receiver - "0000000000000000000000000000000000000001", + "1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e", // zero for one "00", // exact out From cfce36486a812f784d6865506dd501a116592596 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 23:21:57 +0530 Subject: [PATCH 07/16] chore: add comment --- foundry/test/executors/UniswapV2Executor.t.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index 070bc21..f157ee1 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -95,6 +95,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { } function testSwapExecutorEncoderData() public { + // Generated by the strategy encoder bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f564000000000000000000000000000000000000000010000"; console.log(protocolData.length); @@ -109,6 +110,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { } function testSwapExecutorSwap() public { + // Generated by the strategy encoder bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb111d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e0000"; uint256 amountIn = 10 ** 18; From a50c31203b1e44a796469b03eee7c771d661396f Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 23:26:36 +0530 Subject: [PATCH 08/16] chore: foundry fmt --- foundry/test/executors/UniswapV2Executor.t.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index f157ee1..99a6c80 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.28; import "@src/executors/UniswapV2Executor.sol"; import {Test} from "../../lib/forge-std/src/Test.sol"; import {Constants} from "../Constants.sol"; -import {console} from "forge-std/console.sol"; + contract UniswapV2ExecutorExposed is UniswapV2Executor { function decodeParams(bytes calldata data) external @@ -98,7 +98,6 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { // Generated by the strategy encoder bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f564000000000000000000000000000000000000000010000"; - console.log(protocolData.length); (IERC20 tokenIn, address target, address receiver, bool zeroForOne) = uniswapV2Exposed.decodeParams(protocolData); From 1b8bf56c754254dc74233e28f3ae3a3992bbf0d3 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 30 Jan 2025 18:07:07 +0530 Subject: [PATCH 09/16] feat: resolve pr comments --- .../test/executors/UniswapV2Executor.t.sol | 4 ++-- src/encoding/evm/strategy_encoder/encoder.rs | 21 ++++++++++--------- src/encoding/evm/strategy_encoder/selector.rs | 4 ++-- src/encoding/models.rs | 3 +-- src/encoding/strategy_encoder.rs | 4 ++-- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index 99a6c80..4f82d24 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -95,7 +95,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { } function testSwapExecutorEncoderData() public { - // Generated by the strategy encoder + // Generated by the ExecutorStrategyEncoder - test_executor_encoder bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f564000000000000000000000000000000000000000010000"; @@ -109,7 +109,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { } function testSwapExecutorSwap() public { - // Generated by the strategy encoder + // Generated by the ExecutorStrategyEncoder - test_executor_encoder bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb111d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e0000"; uint256 amountIn = 10 ** 18; diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index b12a50e..12cac05 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -2,6 +2,7 @@ use std::str::FromStr; use alloy_primitives::Address; use alloy_sol_types::SolValue; +use tycho_core::Bytes; use crate::encoding::{ errors::EncodingError, @@ -29,7 +30,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder { pub struct SplitSwapStrategyEncoder {} impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} impl StrategyEncoder for SplitSwapStrategyEncoder { - fn encode_strategy(&self, _solution: Solution) -> Result<(Vec, Address), EncodingError> { + fn encode_strategy(&self, _solution: Solution) -> Result<(Vec, Bytes), EncodingError> { todo!() } fn selector(&self, _exact_out: bool) -> &str { @@ -39,10 +40,10 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { /// This strategy encoder is used for solutions that are sent directly to the pool. /// Only 1 solution with 1 swap is supported. -pub struct ExecutorEncoder {} -impl EVMStrategyEncoder for ExecutorEncoder {} -impl StrategyEncoder for ExecutorEncoder { - fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Address), EncodingError> { +pub struct ExecutorStrategyEncoder {} +impl EVMStrategyEncoder for ExecutorStrategyEncoder {} +impl StrategyEncoder for ExecutorStrategyEncoder { + fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Bytes), EncodingError> { if solution.router_address.is_none() { return Err(EncodingError::InvalidInput( "Router address is required for straight to pool solutions".to_string(), @@ -72,7 +73,7 @@ impl StrategyEncoder for ExecutorEncoder { let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?; let executor_address = Address::from_str(swap_encoder.executor_address()) .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?; - Ok((protocol_data, executor_address)) + Ok((protocol_data, Bytes::from(executor_address.as_slice()))) } fn selector(&self, _exact_out: bool) -> &str { "swap(uint256, bytes)" @@ -90,7 +91,7 @@ mod tests { #[test] fn test_executor_encoder() { - let encoder = ExecutorEncoder {}; + let encoder = ExecutorStrategyEncoder {}; let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"); let token_out = Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f"); @@ -114,6 +115,7 @@ mod tests { checked_token: token_out, check_amount: None, sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(), + // The receiver was generated with `makeAddr("bob") using forge` receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(), swaps: vec![swap], straight_to_pool: true, @@ -126,10 +128,9 @@ mod tests { .encode_strategy(solution) .unwrap(); let hex_protocol_data = encode(&protocol_data); - println!("{}", hex_protocol_data); assert_eq!( executor_address, - Address::from_str("0x5c2f5a71f67c01775180adc06909288b4c329308").unwrap() + Bytes::from_str("0x5c2f5a71f67c01775180adc06909288b4c329308").unwrap() ); assert_eq!( hex_protocol_data, @@ -150,7 +151,7 @@ mod tests { #[test] fn test_selector() { - let encoder = ExecutorEncoder {}; + let encoder = ExecutorStrategyEncoder {}; assert_eq!(encoder.selector(false), "swap(uint256, bytes)"); } } diff --git a/src/encoding/evm/strategy_encoder/selector.rs b/src/encoding/evm/strategy_encoder/selector.rs index 5ede40a..74cbabd 100644 --- a/src/encoding/evm/strategy_encoder/selector.rs +++ b/src/encoding/evm/strategy_encoder/selector.rs @@ -1,5 +1,5 @@ use crate::encoding::{ - evm::strategy_encoder::encoder::{ExecutorEncoder, SplitSwapStrategyEncoder}, + evm::strategy_encoder::encoder::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder}, models::Solution, strategy_encoder::{StrategyEncoder, StrategySelector}, }; @@ -9,7 +9,7 @@ pub struct EVMStrategySelector; impl StrategySelector for EVMStrategySelector { fn select_strategy(&self, solution: &Solution) -> Box { if solution.straight_to_pool { - Box::new(ExecutorEncoder {}) + Box::new(ExecutorStrategyEncoder {}) } else { Box::new(SplitSwapStrategyEncoder {}) } diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 7a93105..a31ecaa 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -1,4 +1,3 @@ -use alloy_primitives::Address; use num_bigint::BigUint; use tycho_core::{dto::ProtocolComponent, Bytes}; @@ -62,7 +61,7 @@ pub struct Transaction { // ETH value to be sent with the transaction. pub value: BigUint, // Address of the contract to call with the calldata - pub to: Address, + pub to: Bytes, } #[allow(dead_code)] diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index fb3aa41..fd2e757 100644 --- a/src/encoding/strategy_encoder.rs +++ b/src/encoding/strategy_encoder.rs @@ -1,10 +1,10 @@ -use alloy_primitives::Address; +use tycho_core::Bytes; use crate::encoding::{errors::EncodingError, models::Solution}; #[allow(dead_code)] pub trait StrategyEncoder { - fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec, Address), EncodingError>; + fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec, Bytes), EncodingError>; fn selector(&self, exact_out: bool) -> &str; } From 4ef9b0b82fe8a531b11186d492856a7717fbc4cf Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 30 Jan 2025 18:13:47 +0530 Subject: [PATCH 10/16] chore: use Bytes --- src/encoding/evm/strategy_encoder/encoder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index 12cac05..f7fe0f4 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -71,9 +71,9 @@ impl StrategyEncoder for ExecutorStrategyEncoder { router_address, }; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?; - let executor_address = Address::from_str(swap_encoder.executor_address()) + let executor_address = Bytes::from_str(swap_encoder.executor_address()) .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?; - Ok((protocol_data, Bytes::from(executor_address.as_slice()))) + Ok((protocol_data, executor_address)) } fn selector(&self, _exact_out: bool) -> &str { "swap(uint256, bytes)" From 746733056cb1ed08a28454098c7a4353e3291320 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 30 Jan 2025 19:14:01 +0530 Subject: [PATCH 11/16] chore: encoder test naming --- foundry/test/executors/UniswapV2Executor.t.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index 4f82d24..ed4631f 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -95,7 +95,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { } function testSwapExecutorEncoderData() public { - // Generated by the ExecutorStrategyEncoder - test_executor_encoder + // Generated by the ExecutorStrategyEncoder - test_executor_strategy_encode bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f564000000000000000000000000000000000000000010000"; @@ -109,7 +109,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { } function testSwapExecutorSwap() public { - // Generated by the ExecutorStrategyEncoder - test_executor_encoder + // Generated by the ExecutorStrategyEncoder - test_executor_strategy_encode bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb111d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e0000"; uint256 amountIn = 10 ** 18; From 95d45975adf9f4b35c3746d4281cb08cdbe0f04d Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 30 Jan 2025 19:14:22 +0530 Subject: [PATCH 12/16] chore: encoder test naming --- src/encoding/evm/strategy_encoder/encoder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index f7fe0f4..e9c50f9 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -90,7 +90,7 @@ mod tests { use crate::encoding::models::Swap; #[test] - fn test_executor_encoder() { + fn test_executor_strategy_encode() { let encoder = ExecutorStrategyEncoder {}; let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"); From 8cb95f0950e3b57ae0c6ecc3f4c0950005ae75e7 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 30 Jan 2025 20:48:35 +0530 Subject: [PATCH 13/16] feat: remove exact_out from USV2 --- foundry/src/executors/UniswapV2Executor.sol | 3 ++- foundry/test/executors/UniswapV2Executor.t.sol | 8 +++++--- src/encoding/evm/strategy_encoder/encoder.rs | 2 -- src/encoding/evm/swap_encoder/encoders.rs | 3 --- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/foundry/src/executors/UniswapV2Executor.sol b/foundry/src/executors/UniswapV2Executor.sol index 16bef82..e62a99b 100644 --- a/foundry/src/executors/UniswapV2Executor.sol +++ b/foundry/src/executors/UniswapV2Executor.sol @@ -4,6 +4,7 @@ pragma solidity ^0.8.28; import "@interfaces/IExecutor.sol"; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import "@uniswap-v2/contracts/interfaces/IUniswapV2Pair.sol"; +import "forge-std/console.sol"; error UniswapV2Executor__InvalidDataLength(); @@ -43,7 +44,7 @@ contract UniswapV2Executor is IExecutor { bool zeroForOne ) { - if (data.length < 61) { + if (data.length != 61) { revert UniswapV2Executor__InvalidDataLength(); } inToken = IERC20(address(bytes20(data[0:20]))); diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index ed4631f..9372a5d 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -80,7 +80,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { assertGe(amountOut, 0); } - function testSwap() public { + function testSwapUniswapV2() public { uint256 amountIn = 10 ** 18; uint256 amountOut = 1847751195973566072891; bool zeroForOne = false; @@ -97,7 +97,8 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { function testSwapExecutorEncoderData() public { // Generated by the ExecutorStrategyEncoder - test_executor_strategy_encode bytes memory protocolData = - hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f564000000000000000000000000000000000000000010000"; + hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f5640000000000000000000000000000000000000000100"; + console.log("protocolData1", protocolData.length); (IERC20 tokenIn, address target, address receiver, bool zeroForOne) = uniswapV2Exposed.decodeParams(protocolData); @@ -111,7 +112,8 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { function testSwapExecutorSwap() public { // Generated by the ExecutorStrategyEncoder - test_executor_strategy_encode bytes memory protocolData = - hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb111d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e0000"; + hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb111d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e00"; + console.log("protocolData2", protocolData.length); uint256 amountIn = 10 ** 18; uint256 amountOut = 1847751195973566072891; deal(WETH_ADDR, address(uniswapV2Exposed), amountIn); diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index e9c50f9..a04cddb 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -143,8 +143,6 @@ mod tests { "1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e", // zero for one "00", - // exact out - "00", )) ); } diff --git a/src/encoding/evm/swap_encoder/encoders.rs b/src/encoding/evm/swap_encoder/encoders.rs index f4fbf24..841089d 100644 --- a/src/encoding/evm/swap_encoder/encoders.rs +++ b/src/encoding/evm/swap_encoder/encoders.rs @@ -46,7 +46,6 @@ impl SwapEncoder for UniswapV2SwapEncoder { component_id, bytes_to_address(&encoding_context.receiver)?, zero_to_one, - encoding_context.exact_out, ); Ok(args.abi_encode_packed()) @@ -211,8 +210,6 @@ mod tests { "0000000000000000000000000000000000000001", // zero for one "00", - // exact out - "00", )) ); } From 9c35da07608afc864934409140200c847110e1a7 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 30 Jan 2025 21:06:30 +0530 Subject: [PATCH 14/16] chore: rename straight_to_pool to direct_execution --- src/encoding/evm/router_encoder.rs | 2 +- src/encoding/evm/strategy_encoder/encoder.rs | 2 +- src/encoding/evm/strategy_encoder/selector.rs | 2 +- src/encoding/models.rs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/encoding/evm/router_encoder.rs b/src/encoding/evm/router_encoder.rs index a564e3e..14075c1 100644 --- a/src/encoding/evm/router_encoder.rs +++ b/src/encoding/evm/router_encoder.rs @@ -34,7 +34,7 @@ impl RouterEncoder for EVMRo let mut transactions: Vec = Vec::new(); for solution in solutions.iter() { let exact_out = solution.exact_out; - let straight_to_pool = solution.straight_to_pool; + let straight_to_pool = solution.direct_execution; let strategy = self .strategy_selector diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index a04cddb..09525a2 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -118,7 +118,7 @@ mod tests { // The receiver was generated with `makeAddr("bob") using forge` receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(), swaps: vec![swap], - straight_to_pool: true, + direct_execution: true, router_address: Some(Bytes::zero(20)), slippage: None, native_action: None, diff --git a/src/encoding/evm/strategy_encoder/selector.rs b/src/encoding/evm/strategy_encoder/selector.rs index 74cbabd..3ac619d 100644 --- a/src/encoding/evm/strategy_encoder/selector.rs +++ b/src/encoding/evm/strategy_encoder/selector.rs @@ -8,7 +8,7 @@ pub struct EVMStrategySelector; impl StrategySelector for EVMStrategySelector { fn select_strategy(&self, solution: &Solution) -> Box { - if solution.straight_to_pool { + if solution.direct_execution { Box::new(ExecutorStrategyEncoder {}) } else { Box::new(SplitSwapStrategyEncoder {}) diff --git a/src/encoding/models.rs b/src/encoding/models.rs index a31ecaa..a0daeb0 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -23,10 +23,10 @@ pub struct Solution { pub receiver: Bytes, /// List of swaps to fulfill the solution. pub swaps: Vec, - /// If set to true, the solution will be encoded to be sent directly to the SwapExecutor and + /// If set to true, the solution will be encoded to be sent directly to the Executor and /// skip the router. The user is responsible for managing necessary approvals and token /// transfers. - pub straight_to_pool: bool, + pub direct_execution: bool, // if not set, then the Propeller Router will be used pub router_address: Option, // if set, it will be applied to check_amount From da7b02407a104ae485e7f9f47c269e978bcde2dd Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 30 Jan 2025 21:11:24 +0530 Subject: [PATCH 15/16] chore: remove console --- foundry/src/executors/UniswapV2Executor.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/foundry/src/executors/UniswapV2Executor.sol b/foundry/src/executors/UniswapV2Executor.sol index e62a99b..9f447f1 100644 --- a/foundry/src/executors/UniswapV2Executor.sol +++ b/foundry/src/executors/UniswapV2Executor.sol @@ -4,7 +4,6 @@ pragma solidity ^0.8.28; import "@interfaces/IExecutor.sol"; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import "@uniswap-v2/contracts/interfaces/IUniswapV2Pair.sol"; -import "forge-std/console.sol"; error UniswapV2Executor__InvalidDataLength(); From 6c8226340d068fa498205f07e426aee7b3a7e3ce Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 30 Jan 2025 21:13:15 +0530 Subject: [PATCH 16/16] chore: remove console --- foundry/test/executors/UniswapV2Executor.t.sol | 2 -- 1 file changed, 2 deletions(-) diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index 9372a5d..9c7066f 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -98,7 +98,6 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { // Generated by the ExecutorStrategyEncoder - test_executor_strategy_encode bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f5640000000000000000000000000000000000000000100"; - console.log("protocolData1", protocolData.length); (IERC20 tokenIn, address target, address receiver, bool zeroForOne) = uniswapV2Exposed.decodeParams(protocolData); @@ -113,7 +112,6 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { // Generated by the ExecutorStrategyEncoder - test_executor_strategy_encode bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb111d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e00"; - console.log("protocolData2", protocolData.length); uint256 amountIn = 10 ** 18; uint256 amountOut = 1847751195973566072891; deal(WETH_ADDR, address(uniswapV2Exposed), amountIn);